美文网首页
根据年月日计算星期几。

根据年月日计算星期几。

作者: asmao | 来源:发表于2022-02-08 10:46 被阅读0次

根据年月日计算星期几
运用蔡勒公式

//比如2022年2月8号
NSInteger year = 2022;
NSInteger month = 2;
NSInteger day = 8;

NSInteger c = year/100; //世纪-1
NSInteger y = year%100; //年份后两位
NSInteger m = month;//月份如果是1月或者2月 年份要退一位 月份加12 
//例如是 2021年13月代表 2022年1月 2021年14月代表2022年2月。
if (m < 3) {
    m +=12;
    y -=1;
}

NSInteger week = c/4 - 2*c + y + y/4 + 13*(m+1)/5 + day - 1;
NSInteger weekIndex = week%7; 
//weekIndex = 0  代表星期日
NSLog(@"星期 %ld ",weekIndex);

相关文章

网友评论

      本文标题:根据年月日计算星期几。

      本文链接:https://www.haomeiwen.com/subject/ailbkrtx.html