mktime() - Time Date Function

How to convert date to calendar in C++?

Explanation

mktime() function converts the calendar time equivalent of the broken down structure pointed by "time". The "tm_wday", "tm_yday" elements are set by the function itself.

Syntax:


time_t mktime(struct tm *time);

Example :



#include <stdio.h>
#include <time.h>
int main ()
{
time_t tim;
struct tm *ptr;
int y = 2010, m = 2 ,d = 2;
char * weekday[] = { "Sunday", "Monday", "Tuesday",
"Wednesday", "Thursday", "Friday", "Saturday"};
time ( &tim );
ptr = localtime ( &tim );
ptr->tm_year = y - 1900;
ptr->tm_mon = m - 1;
ptr->tm_mday = d;
mktime ( ptr );
printf ("February 2, 2010 was a:: %s.\n",
weekday[ptr->tm_wday]);
return 0;
}

Result :

February 2, 2010 was a:: Tuesday

In the above example "mktime()" is used to convert the "tm_wday" to find the weekday for the given date in calendar.

C++ Tutorial


Ask Questions

Ask Question