美文网首页
计算日期的不常用算法

计算日期的不常用算法

作者: 马戏团小丑 | 来源:发表于2018-04-13 11:39 被阅读16次
    • 计算month个月后的最后一天
    - (NSDate *)getEndDateWithMonth:(int)month{
        NSCalendar* cal=[NSCalendar currentCalendar];
        NSDateComponents *dateComponents = [cal components:NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay fromDate:[NSDate date]];
        dateComponents.day =1;
        dateComponents.month +=month;
        NSDate * endDayOfNextMonth = [cal dateFromComponents:dateComponents];
        endDayOfNextMonth = [endDayOfNextMonth dateByAddingTimeInterval:-1];
        return endDayOfNextMonth;
    }
    
    • 获取某个月的所有的date,如果是str=@"",计算的是今天到月底的date,str以yyyyMMdd传递
    - (NSArray <NSDate *>*)getMonthBeginAndEnd:(NSString *)str{
        NSDate *nowDate;
        if (str.length == 0) {
            nowDate =[NSDate date];
        }else{
            nowDate = [ProcessingTime dateWithDateString:str andFormatString:@"yyyyMMdd"];
        }
        
        NSMutableArray *dateArr = [[NSMutableArray alloc]initWithObjects:nowDate, nil];
        
        double interval = 0;
        NSDate *beginDate = nil;
        NSDate *endDate = nil;
        NSCalendar *calendar = [NSCalendar currentCalendar];
        [calendar setFirstWeekday:1];//设定周日为周首日
        BOOL ok = [calendar rangeOfUnit:NSCalendarUnitMonth startDate:&beginDate interval:&interval forDate:nowDate];
        //分别修改为 NSDayCalendarUnit NSWeekCalendarUnit NSYearCalendarUnit
        if (ok) {
            endDate = [beginDate dateByAddingTimeInterval:interval-1];
        }else {
            return nil;
        }
        
        long long nowTime = [[ProcessingTime timeStampWithDate:nowDate] longLongValue];
        long long endTime = [[ProcessingTime timeStampWithDate:endDate]longLongValue];
    
        long long dayTime = 24*60*60;
        long long time = nowTime + dayTime;
        NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
        [formatter setDateFormat:@"yyyyMMdd"];
        while (time <= endTime) {
            NSString *timeString = [formatter stringFromDate:[NSDate dateWithTimeIntervalSince1970:time]];
            [dateArr addObject:[formatter dateFromString:timeString]];
            time += dayTime;
        }
        return dateArr;
    }
    
    • 获取今天是周几
    - (NSString *)weekdayStringFromDate{
        NSArray *weekdays = [NSArray arrayWithObjects: [NSNull null], @"星期天", @"星期一", @"星期二", @"星期三", @"星期四", @"星期五", @"星期六", nil];
        NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
        NSTimeZone *timeZone = [[NSTimeZone alloc] initWithName:@"Asia/Shanghai"];
        [calendar setTimeZone: timeZone];
        NSCalendarUnit calendarUnit = NSCalendarUnitWeekday;
        NSDateComponents *theComponents = [calendar components:calendarUnit fromDate:self.calendar.today];
        return [weekdays objectAtIndex:theComponents.weekday];
    }
    

    相关文章

      网友评论

          本文标题:计算日期的不常用算法

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