从给定日期开始的星期几

Day of the week from a given date

本文关键字:开始 日期      更新时间:2023-10-16

我需要从给定日期找到工作日;我有以下代码但不起作用

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;
}

我在这里做错了什么?

你应该拿struct tm tm;而不是struct tm * tm;

您需要使用 memset(&tm,0x00,sizeof(tm)); 初始化tm否则mktime将返回 -1

> 日期 27/02/2014 的结果4是正确的,请参阅time.h

描述
...
int tm_wday 星期几 [0,6](星期日 =0)。

星期日是0,星期一= 1,星期二= 2,星期三= 3,星期四= 4,...