0

Can someone help me with this program.I had found that the output of this program should be the local time and display time and date and i dont know where my program gone wrong and display like this
time is 03:29:30
date08/06/108

how to eliminate 1 from my program.it should be "date 08/06/08"..please help me..

#include<stdio.h>
#include<time.h>

int main(void)
{
    struct tm *systime;
    time_t t;
    
    t=time(NULL);
    systime = localtime(&t);
    
    printf("time is %.2d:%.2d:%.2d\n",systime->tm_hour,systime->tm_min,systime->tm_sec);
    printf("Date:%.2d/%.2d/%.2d",systime->tm_mon+1,
    systime->tm_mday,systime->tm_year);
    
    return 0;
}
3
Contributors
2
Replies
3
Views
9 Years
Discussion Span
Last Post by Aia
0

Look up the specification of localtime(). My recollection is that systime->tm_year would be the number of years since 1900. 2008-1900, last I checked, yielded a value of 108.

0

>how to eliminate 1 from my program.it should be "date 08/06/08"..please help me..

These values returned to the variable t are not quite ready to show in prime time yet.
They are correct but some need to be formatted to show like you want.
Functions like asctime() or ctime() can return a string in a predetermined format.
To display the date in the specific way you asked, the function strftime() can do it, counting that you pass to it the right arguments, that you set a correct size buffer to receive the string, and assuming you give it the right format place holders.

char buffer[9]; /* enough to contain the string "mm/dd/yy" plus '\0' */
.
.
.
if ( strftime(buffer, sizeof(buffer), "%m/%d/%y", systime) ) {
    printf("date %s\n", buffer);
}
This topic has been dead for over six months. 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.