C / C ++ - 检查今天是否是本月的第一个星期一

时间:2013-07-10 09:21:56

标签: c++ c date calendar

如何使用C / C ++代码检查今天是否是本月的第一个星期一?

使用Java和C#更简单(如下面的链接所示)。

任何人都可以帮助我使用C / C ++实现这一目标。

c-sharp-how-can-i-check-if-today-is-the-first-monday-of-the-month

java check if date is first Sunday of the Month

3 个答案:

答案 0 :(得分:4)

这应该是你要找的东西:

#include <iostream>
#include <ctime>

int main(){
   std::time_t result = std::time(NULL);
   const std::tm* t =  std::localtime(&result);
   if(t->tm_wday == 1 and t->tm_mday <= 7)
    std::cout << "true" << std::endl;
    else
    std::cout << "false" << std::endl;
}

代码已经过测试here

答案 1 :(得分:2)

使用boost.date_time gregorian。有一个函数day_clock::local_day()可以为您提供今天的日期。然后,您可以使用day()成员函数查询月中的某一天和day_of_week()成员,以查看它是否是星期一。剩下的就像你链接到的C#样本一样。

答案 2 :(得分:1)

您可以使用time()localtime()来检索struct time *(让我们将其命名为tp)。那么今天是本月的第一个星期一,当且仅当tp->tm_mday <= 7(以1开头)和tp->tm_wday == 1(0 =星期日等)

相关问题