Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6863865
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T02:50:23+00:00 2026-05-27T02:50:23+00:00

The question is simple, there are many API specific methods in other languages, but

  • 0

The question is simple, there are many API specific methods in other languages, but I found none that that were simple and straight forward for Cross-platform C++ usage.

If I have two dates, and they are assumed to be in the same locale, what is the easiest way to differentiate them in C++?

I have (somewhat) looked at using time.h, creating two tm objects, converting them to UTC and then differentiating them.

The current format for the dates is: YY/MM/DD HH:mm:SS (stored as individual integers)

EDIT:

Ok, based on current answers, I’ve done the following (for now):

time_t calculate_seconds_between(
    const uint Y1, const uint M1, const uint D1, const uint H1, const uint m1, const uint S1, // YY/MM/DD HH:mm:SS
    const uint Y2, const uint M2, const uint D2, const uint H2, const uint m2, const uint S2
)
{
    time_t raw;
    time(&raw);

    struct tm t1 = *gmtime(&raw), t2 = t1;

    t1.tm_year = Y1 - 1900;
    t1.tm_mon = M1 - 1;
    t1.tm_mday = D1;
    t1.tm_hour = H1;
    t1.tm_min = m1;
    t1.tm_sec = S1;

    t2.tm_year = Y2 - 1900;
    t2.tm_mon = M2 - 1;
    t2.tm_mday = D2;
    t2.tm_hour = H2;
    t2.tm_min = m2;
    t2.tm_sec = S2;

    time_t tt1, tt2;
    tt1 = mktime(&t1);
    tt2 = mktime(&t2);

    return (tt2 - tt1);
}

Which works great.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T02:50:24+00:00Added an answer on May 27, 2026 at 2:50 am

    To convert from YY/MM/DD HH:mm:SS to struct tm you can use strptime (or make an equivalent if you don’t have it)

    const char * dateInput;
    // ....
    struct tm tm;
    time_t val;
    
    memset(&tm, 0, sizeof(struct tm));
    strptime(dateInput,"%y/%m/%d %H:%M:%S",&tm);
    val = mktime(&tm);
    

    After you have the two time_t values you can call difftime.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This should be very simple question. There are many programming languages out there, compiled
Simple question, but one that I've been curious about...is there a functional difference between
Sorry for the simple question but I feel like there's a smarter way to
There seem to be many questions in around this topic but none seem to
The question is more simple than what it looks. There are many use cases
It looks a very simple question. There are many lines available as their two
This is going to be a simple question to many of you, but I
My question is very simple: Is there any solution to install xCode, or equivalent,
This is a simple question : Is there any automatic indentation alignment on Eclipse
Simple question here: is there any way to convert from a jagged array to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.