Reputation: 480
I am kinda stuck here with the mktime function and I would appreciate any help I can get.
So what I am doing is, having the user input a year, month and day and then run the tm structure to calculate everything like this:
struct tm time;
time_t t;
time.tm_year = year - 1900;
time.tm_mon = month - 1;
time.tm_mday = day;
time.tm_hour = 0;
time.tm_min = 0;
time.tm_sec = 0;
time.tm_isdst = -1;
And then I run the mktime function and set it equal to t:
t = mktime(&time);
And then I run the print function like this:
printf("%s", ctime(&t));
However, what I am getting via this method is this for example:
Sat Aug 23 00:00:00 2013
But what I would like the final time format to look like is this:
Mon 2013 02 11
How would I go about doing this? I think my approach with the ctime function is not really right.
Maybe I should use the strftime function?
Note that I don't need to print the current time though.
Thanks, have a good one : )
Upvotes: 1
Views: 499
Reputation: 1518
You can use strftime
for this. Here is an example:
char str[50];
struct tm *tm_info = localtime(&t);
strftime(str, 50, "%a %G %m %d", tm_info);
Then you can use printf("%s\n", str)
to output the formatted date.
Thanks to Xis88 for the format.
Upvotes: 3