【问题标题】:How to calculate the ISO week day using the ordinal day and the day of the week of the 1st of January?如何使用序号日和 1 月 1 日的星期几计算 ISO 工作日?
【发布时间】:2019-03-30 02:55:40
【问题描述】:

我需要计算特定日期的 ISO 工作日。我目前有计算序数日的函数,以及计算每年 1 月 1 日这一天的函数,但是,我现在需要一个函数来计算任何日期的 ISO 工作日。

我应该使用序号天、星期几和模 7 算术来执行此操作,但我不确定如何执行此操作,例如,对于 2008 年 9 月 26 日星期五的日期,我的序号天应该是 270(它是),我的工作日号码应该是 5(星期五),但不是。我想我应该将两者与模 7 算术结合起来,但我不确定如何。

我试过做 270 % 7 得到 4 的答案,但它应该是 5。我不完全确定我应该如何将这两个值放在一起(如果我什至需要的话)

我的两个函数的代码:

int day_of_the_week(int year)
{
int week_day;
week_day = ((1 + 5 * ((year - 1) % 4) + 4 * ((year - 1) % 100) + 6 * ((year 
- 1) % 400) ) % 7);
if (week_day == 0) {
    week_day = 7;
}
return 0;
}


int calculate_ordinal_day(int day, int month, int year, int isyearleap) {
int ordinal_day;

if (month == 1) {
    return day;
}
if (month == 2) {
    return day + 31;
}

ordinal_day = myFloor(30.6 * month - 91.4) + day;
if (isyearleap == 1) {
    return ordinal_day + 60;
}
return ordinal_day + 59;
}

【问题讨论】:

  • 你读过this吗?

标签: c date math iso weekday


【解决方案1】:

为什么你认为Friday 应该是5?你正在做模7,所以可能的结果是01234560 是第一天,Monday6 最后一天,SundayFriday 正确编号为 4。如果您希望 Monday 成为 1Sunday 成为 7,只需将 1 添加到结果中。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-05-09
    • 1970-01-01
    • 2018-07-14
    • 2017-04-14
    • 1970-01-01
    • 1970-01-01
    • 2018-04-03
    • 1970-01-01
    相关资源
    最近更新 更多