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 6350657
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T21:51:15+00:00 2026-05-24T21:51:15+00:00

Im trying to convert a string such as 12 August 2011 into time_t or

  • 0

Im trying to convert a string such as “12 August 2011” into time_t or that seconds elapsed, or whatever so I can use it to compare a list of dates.

At the moment, I have tried the following but the output seems to equal false! Also, the seconds elapsed seem to keep changing?

Is this correct?

#include <iostream>
#include <string>
#include <cstdlib>
#include <cstring>
#include <time.h>
#include <stdio.h>

using namespace std;

int main()
{

    struct tm tmlol, tmloltwo;
    time_t t, u;

    t = mktime(&tmlol);
    u = mktime(&tmloltwo);
    //char test[] = "01/01/2008";string test = "01/01/2008";

    strptime("10 February 2010", "%d %b %Y", &tmlol);
    strptime("10 February 2010", "%d %b %Y", &tmloltwo);

    t = mktime(&tmlol);
    u = mktime(&tmloltwo);

    cout << t << endl;
    cout << u << endl;

    if (u>t)
    {
        cout << "true" << endl;
    }
    else if (u==t)
    {
        cout << "same" << endl;
    }
    else
    {
        cout << "false" << endl;
    }

    cout << (u-t);
}
  • 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-24T21:51:17+00:00Added an answer on May 24, 2026 at 9:51 pm

    You should initialize the structs before use. Try this:

    #include <iostream> 
    #include <string>
    #include <cstdlib>
    #include <cstring>
    #include <time.h>
    #include <stdio.h>          
    
    using namespace std;
    
    int main()
    {
        struct tm tmlol, tmloltwo;
        time_t t, u;
    
        // initialize declared structs
        memset(&tmlol, 0, sizeof(struct tm));
        memset(&tmloltwo, 0, sizeof(struct tm));
    
        strptime("10 February 2010", "%d %b %Y", &tmlol);         
        strptime("10 February 2010", "%d %b %Y", &tmloltwo);
    
        t = mktime(&tmlol);
        u = mktime(&tmloltwo);
    
        cout << t << endl;
        cout << u << endl;
    
        if (u>t)
        {
            cout << "true" << endl;
        }
        else if (u==t)
        {
            cout << "same" << endl;
        }
        else
        {
            cout << "false" << endl;
        }
    
        cout << (u-t) << endl;
    
        return 0;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to use the STL function for_each to convert a string into
I am trying to convert a string value into a name of an attribute
I'm trying to convert the String \something\ into the String \\something\\ using replaceAll ,
I'm trying to convert a date string into an age. The string is like:
i am trying to convert a LPCSTR string into LPCTSTR string. i want to
I'm trying to convert CLLocation latitude / longitude to a string. I can successfully
I have a MongoMapper model and am trying to convert a comma-delimited string into
I am trying to convert a String into an ArrayList. For example, my Struts2
I am trying to convert bitmap images into a base64 string before inserting it
I'm trying to take a variable (long), and convert it to a string, such

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.