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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T21:20:58+00:00 2026-06-15T21:20:58+00:00

I have 2 functions. First one converts time_t to string. Second one string to

  • 0

I have 2 functions. First one converts time_t to string. Second one string to time_t.
I just have the date that needs to be converted and restored back as string.

The functions are

void split(const string &s, char delim, vector<string>& elems) {
    stringstream ss(s); string item; 
    while(getline(ss, item, delim)) { elems.push_back(item);} return;
}

time_t getDateInTimeTfromHyphenSplitString(string s)
{
    struct tm tmvar = {0};
    vector<string> tim;
    split(s.c_str(),'-',tim);
    tmvar.tm_year = atoi(tim[2].c_str()) - 1900;
    tmvar.tm_mon = atoi(tim[1].c_str());
    tmvar.tm_mday = atoi(tim[0].c_str());
    tmvar.tm_isdst = 0;
    time_t ttm = mktime(&tmvar);
    return ttm;
}

string getDateInHyphenSplitStringfromTimeT(time_t t)
{
    struct tm *timeinfo = (tm*)malloc(sizeof(tm));
    gmtime_s(timeinfo, &t);
    char *buffer = NULL;
    buffer = (char*)malloc((size_t)20);
    strftime(buffer, 20, "%d-%m-%Y", timeinfo);
    string s = buffer ;
    return s;
}

Now when I test this code with following lines the out put seems strange.

string sk = "31-12-2010";
cout << sk << endl;
time_t ttk = getDateInTimeTfromHyphenSplitString(sk);
sk = getDateInHyphenSplitStringfromTimeT(ttk );

cout << sk << endl;

Input :- 31-12-2010
Output:- 30-01-2011

Strangely I am getting 1 month difference for any date that I supply as input.

Please let me know, if how to get the correct time back.

PS: I choose this technique because I need to format date with ‘-‘s.

  • 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-06-15T21:20:59+00:00Added an answer on June 15, 2026 at 9:20 pm

    tm_mon counts the “months since January”, thus ranging in value from 0 to 11.

    You need to subtract 1 from the input month.


    Apart from that, your code leaks memory: You never free your malloced memory.
    Since you are programming in C++, I’d recommend not using malloc/free altogether, but
    new, new[], delete and delete[] instead, as malloc has no notion of objects and doesn’t call constructors.

    In the special case of your function, you wouldn’t even need dynamic memory allocation at all:

        struct tm timeinfo;
        gmtime_s(&timeinfo, &t);
        char buffer[20];
        strftime(buffer, sizeof(buffer), "%d-%m-%Y", &timeinfo);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this function called ProperCase that takes a string, then converts the first
I have two jQuery functions. The first one below I want to run first,
We have a function that pulls the date from the first 8 characters of
I have a function that takes two arguments, the first one $methodName being a
I am learning Javascript and I have read functions are first class objects and
Assuming I have the following two JQuery functions - The first, which works: $(#myLink_931).click(function
Alright this is my first day with JQuery so have fun with these functions
I have a little problem with some preg_replace functions in PHP. First I have
I have a quick question here. I know that the cakePHP find('first') function returns
I have a JSON character string that I put into a data frame. I

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.