Thulasi
Thulasi

Reputation: 23

Day of the week from a given date

I need to find the weekday from the given date; I have following code but does not work

int day;
char *str ="25/02/2014";  
struct tm tm;

if (strptime(str, "%d/%m/%Y", &tm) != NULL)
{
    time_t t = mktime(&tm);
    day = localtime(&t)->tm_wday;
    return day;
}

What am I doing wrong here?

Upvotes: 0

Views: 1715

Answers (2)

Olaf Dietsche
Olaf Dietsche

Reputation: 74028

The result 4 for the date 27/02/2014 is correct, see time.h

Description
...
int tm_wday Day of week [0,6] (Sunday =0).

Sunday is 0, Monday = 1, Tuesday = 2, Wednesday = 3 and Thursday = 4, ...

Upvotes: 0

rajenpandit
rajenpandit

Reputation: 1361

You should take struct tm tm; instead of struct tm * tm;

you need to initialize tm by using memset(&tm,0x00,sizeof(tm)); otherwise mktime will return -1

Upvotes: 2

Related Questions