美文网首页
iOS开发之时间计算

iOS开发之时间计算

作者: Streamsle | 来源:发表于2022-01-13 11:40 被阅读0次

    1、获取当前时间

    //获取当前时间,时间格式yyyyMMddHHmmssSSS
    -(NSString * )getCurrentHourAndMinuteTime{
    
        NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    
        // 设置想要的格式,hh与HH的区别:分别表示12小时制,24小时制
    
        [formatter setDateFormat:@"yyyyMMddHHmmssSSS"];
    
        NSDate *dateNow = [NSDate date];
    
        //把NSDate按formatter格式转成NSString
    
        NSString *currentTime = [formatter stringFromDate:dateNow];
    
        return currentTime;
    
    }
    

    2、计算两个时间点的时间差,精确到毫秒,时间格式yyyyMMddHHmmssSSS

    //获取两个时间点的时间差,精确到毫秒
    -(double )getTimeDifferenceWithStartTime:(NSString * )startTime andEndTime:(NSString *)endTime{
    
        NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    
        [dateFormatter setDateFormat:@"yyyyMMddHHmmssSSS"];
    
        NSDate* startTimeData = [dateFormatter dateFromString:startTime];
    
        NSTimeInterval startTimeSp=[startTimeData timeIntervalSince1970]*1000;// *1000 是精确到毫秒,不乘就是精确到秒
    
        NSDate* endTimeData = [dateFormatter dateFromString:endTime];
    
        NSTimeInterval endTimeSp=[endTimeData timeIntervalSince1970]*1000;
    
        double difference = (endTimeSp - startTimeSp);
    
        NSLog(@"开始时间:%@,结束时间:%@,两个时间差为%lf秒",startTime,endTime, difference);
    
        return difference;
    
    }
    

    3、获取日期date对应月的第一天日期

    + (NSString *)getMonthFirstDayWithDate:(NSDate *)date format:(NSDateFormatter *)myDateFormatter
    {
        NSDate *newDate = date;
    
        double interval = 0;
    
        NSDate *firstDate = nil;
    
        NSCalendar *calendar = [NSCalendar currentCalendar];
    
        BOOL bl = [calendar rangeOfUnit:NSCalendarUnitMonth startDate:& firstDate interval:&interval forDate:newDate];
    
        if (bl) {
    
        NSString *firstString = [myDateFormatter stringFromDate: firstDate];
    
        return firstString;
    
        }
        return @"";
    }
    

    4、获取日期date对应月的最后一天日期

    + (NSString *)getMonthLastDayWithDate:(NSDate *)date format:(NSDateFormatter *)myDateFormatter
    {
        NSDate *newDate = date;
    
        double interval = 0;
    
        NSDate *firstDate = nil;
    
        NSDate *lastDate = nil;
    
        NSCalendar *calendar = [NSCalendar currentCalendar];
    
        BOOL bl = [calendar rangeOfUnit:NSCalendarUnitMonth startDate:& firstDate interval:&interval forDate:newDate];
    
        if (bl) {
    
        lastDate = [firstDate dateByAddingTimeInterval:interval - 1];
    
        NSString *lastString = [myDateFormatter stringFromDate: lastDate];
    
        return lastString;
    
        }
    
        return @"";
    }
    

    5、获取对应日期是周几

    + (NSInteger)getWeekDayFromDate:(NSDate *)date
    {
        NSArray *tempWeek = @[@"7",@"1",@"2",@"3",@"4",@"5",@"6"];
    
        NSCalendar *calendar = [[NSCalendar alloc] 
    initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
    
        NSDateComponents *comps = [[NSDateComponents alloc] init];
    
        NSInteger unitFlags = NSCalendarUnitYear |NSCalendarUnitMonth | NSCalendarUnitDay |NSCalendarUnitWeekday | NSCalendarUnitHour |NSCalendarUnitMinute |NSCalendarUnitSecond;
    
        comps = [calendar components:unitFlags fromDate:date];
    
        // 1、2、3、4、5、6、7 分别对应 周日、周一、周二、周三、周四、周五、周六
    
        NSInteger week = [comps weekday];
    
        NSLog(@"---%ld",week);
    
        // 调整后 1 代表 周一
    
        return [tempWeek[week-1] integerValue] ;
    }
    

    相关文章

      网友评论

          本文标题:iOS开发之时间计算

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