0
#include <time.h>
time_t whatTime(const char* month, const char* day, const char* year)
{
    time_t rawTime;
    time(&rawTime);
    struct tm* convTime = localtime(&rawTime);
    std::cout << "Today is: " << ((convTime->tm_mon)+1) << "/"
                              << convTime->tm_mday << "/"
                              << convTime->tm_year << std::endl;
}

When using this code I get the output '7/27/111'
Why does the year appear as 111 instead of 11?

3
Contributors
2
Replies
3
Views
6 Years
Discussion Span
Last Post by pseudorandom21
1

Because the date in a "tm" structure is counted from year 1900. Which means that 2011 is stored as 111.

Generally, if you want to print a string from a time_t structure, use the function "ctime". Or, to convert from "tm" structure, use "asctime" or "strftime" for special formatting.

0
#include <time.h>
time_t whatTime(const char* month, const char* day, const char* year)
{
    time_t rawTime;
    time(&rawTime);
    struct tm* convTime = localtime(&rawTime);
    std::cout << "Today is: " << ((convTime->tm_mon)+1) << "/"
                              << convTime->tm_mday << "/"
                              << convTime->tm_year << std::endl;
}

When using this code I get the output '7/27/111'
Why does the year appear as 111 instead of 11?

tm::tm_year is the number of years since 1900, which is 111.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.