Why my print current date time (C language) gives different answer
Posted
by vodkhang
on Stack Overflow
See other posts from Stack Overflow
or by vodkhang
Published on 2010-04-20T12:27:53Z
Indexed on
2010/04/20
12:33 UTC
Read the original article
Hit count: 251
I want to get the current date (day, mon and year). I found out there are some functions in C to do that like ctime (get the string of time), localtime and gmtime. I tried with following code but the output are different. I get this output:
The date and time is Tue Apr 20 2010 (which is correct)
The year is : 110
The year is : 110.
Does anybody know why?
int main(int argc, char** argv)
{
time_t now;
if((now = time(NULL)) == (time_t)-1)
{
puts("Failure in getting time");
}
else {
printf("The date and time is: %s\n", ctime(&now));
printf("The year is: %ld\n", localtime(&now)->tm_year);
printf("The year is: %ld\n", gmtime(&now)->tm_year);
}
getchar();
}
© Stack Overflow or respective owner