美文网首页
学习笔记-计算当前日期为星期几

学习笔记-计算当前日期为星期几

作者: Mystical | 来源:发表于2017-02-08 02:08 被阅读15次
    // 判断是否为闰年
    - (BOOL)judgeIsLeapyears:(NSInteger)year{
        if ((year %4 == 0 && year % 100 != 0) || year % 400 == 0) {
            return YES;
        }else{
            return NO;
        }
    }
    
    - (NSInteger)judgeDate:(NSString *)date{
    // 此处使用的日期格式为xxxx-xx-xx
        NSArray *array = [date componentsSeparatedByString:@"-"];
        NSInteger year = [array[0] integerValue];
        NSInteger month = [array[1] integerValue];
        NSInteger day = [array[2] integerValue];
        BOOL isLeapyear = [self judgeIsLeapyears:year];
        NSInteger FebDays;// 二月天数
        NSInteger n = 0;
        if (isLeapyear) {
            FebDays = 29;
        }else{
            FebDays = 28;
        }
        switch (month) {
            case 1:n=day;break;
            case 2:n=31+day;break;
            case 3:n=31+FebDays+day;break;
            case 4:n=31+FebDays+31+day;break;
            case 5:n=31+FebDays+31+30+day;break;
            case 6:n=31+FebDays+31+30+31+day;break;
            case 7:n=31+FebDays+31+30+31+30+day;break;
            case 8:n=31+FebDays+31+30+31+30+31+day;break;
            case 9:n=31+FebDays+31+30+31+30+31+31+day;break;
            case 10:n=31+FebDays+31+30+31+30+31+31+30+day;break;
            case 11:n=31+FebDays+31+30+31+30+31+31+30+31+day;break;
            case 12:n=31+FebDays+31+30+31+30+31+31+30+31+30+day;break;
        }
        NSInteger num = (year-1)+(year-1)/4-(year-1)/100+(year-1)/400+n;
        return num % 7;// 1-6对应星期一到星期六,0为星期天
    }
    

    相关文章

      网友评论

          本文标题:学习笔记-计算当前日期为星期几

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