美文网首页
当前日期是这个月的第几周

当前日期是这个月的第几周

作者: eb7a9dac29d0 | 来源:发表于2017-09-01 10:40 被阅读0次

    NSInteger  weekNumInMonth = [self getWeekdayOrdinalInMonth];

    DLog(@"weekNumInMonth:%ld",(long)weekNumInMonth);

    if (weekNumInMonth == weekNum) {

    for (int i = 1; i<= weekNum ; i++ )

    {

    muarray [weekNum -i] = [self getFirstAndLastdayinWeek];

    _NumW ++;

    }

    }else if(weekNumInMonth < weekNum) {

    for (int i = 1; i<= weekNumInMonth - 1; i++ )

    {

    _NumW ++;

    muarray [weekNumInMonth - 1 - i] = [self getFirstAndLastdayinWeek];

    }

    _NumW =1;

    for (int i = 0; i< weekNum - weekNumInMonth + 1; i++ )

    {

    muarray [weekNumInMonth - 1 + i ] = [self getFirstAndLastdayinWeek];

    _NumW --;

    }

    }

    // 当前日期是这个月的第几周

    -(NSInteger)getWeekdayOrdinalInMonth{

    NSString *date = @"2017-07-31";

    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];

    dateFormatter.dateFormat = @"yyyy-MM-dd";

    NSDate *nowDate = [dateFormatter dateFromString:date];

    //    NSDate *nowDate = [NSDate date];

    //    DLog(@"[self getMonthDate]: %@",[self getMonthDate]);

    NSCalendar *calendar = [NSCalendar currentCalendar];

    NSDateComponents *comp = [calendar components: NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay | NSCalendarUnitWeekday | NSCalendarUnitDay |NSCalendarUnitWeekdayOrdinal  fromDate:nowDate];

    // 当前日期是这个月的第几周

    NSInteger weekdayOrdinal = [comp weekdayOrdinal];

    return weekdayOrdinal;

    }

    #pragma mark - 获取当前月

    - (NSDate *)getMonthDate

    {

    NSDate *nowDate = [NSDate date];

    NSCalendar *calendar = [NSCalendar currentCalendar];

    NSDateComponents *comp = [calendar components: NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay | NSCalendarUnitWeekday | NSCalendarUnitDay fromDate:nowDate];

    // 获取今天是周几

    NSInteger weekDay = [comp weekday];

    // 获取几天是几号

    NSInteger day = [comp day];

    // 计算当前日期和本周的星期一和星期天相差天数

    long firstDiff,lastDiff;

    if (weekDay == 1)

    {

    firstDiff = -31;

    lastDiff = 0;

    if (_NumM > 1) {

    firstDiff = [calendar firstWeekday] - weekDay + 1 - ((_NumM)*31);

    lastDiff = 31 - weekDay;

    }

    }

    else

    {

    if(_NumM == 0)

    {

    firstDiff = [calendar firstWeekday] - weekDay + 1;

    lastDiff = 31 - weekDay;

    }else

    {

    firstDiff = [calendar firstWeekday] - weekDay + 1 - ((_NumM -1)*31);

    lastDiff = 31 - weekDay;

    }

    }

    NSDateComponents *firstDayComp = [calendar components:NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay  fromDate:nowDate];

    [firstDayComp setDay:day + firstDiff];

    NSDate *firstDayOfWeek = [calendar dateFromComponents:firstDayComp];

    //    DLog(@"firstDayOfWeek: %@",firstDayOfWeek);

    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];

    [formatter setDateFormat:@"yyyy-MM-dd"];

    // 本周第一天时间

    NSString *firstDay = [formatter stringFromDate:firstDayOfWeek];

    //    return firstDay;

    return firstDayOfWeek;

    }

    相关文章

      网友评论

          本文标题:当前日期是这个月的第几周

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