#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?

Recommended Answers

All 2 Replies

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.

#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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.