美文网首页
【iOS】常见NSDate与时间戳转换

【iOS】常见NSDate与时间戳转换

作者: 文子飞_ | 来源:发表于2021-07-30 20:30 被阅读0次
//时间戳转时间 年月日
+ (NSString *)timeStringWithTimeInterval:(NSInteger)timeInterval {
    NSDate *date = [NSDate dateWithTimeIntervalSince1970:timeInterval]; //此处根据项目需求,选择是否除以1000 , 如果时间戳精确到秒则去掉1000
    NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
    //    NSDate *currentDate = [NSDate date];//获取当前时间,日
    //    NSDate *pastHalfHour = [currentDate dateByAddingTimeInterval:-3600]; // 半小时前是-1800   1小时后是3600   1小时前是-3600
    //
    //    NSInteger timeSp = [[NSNumber numberWithDouble:[pastHalfHour timeIntervalSince1970]] integerValue];
    //    if (timeInterval < timeSp) {
    //
    //    }
    formatter.dateFormat = @"YYYY-MM-dd  HH:mm:ss";
    return [formatter stringFromDate:date];
    //今天
    //    if ([date isToday]) {
    //
    //        formatter.dateFormat = @"HH:mm";
    //
    //        return [formatter stringFromDate:date];
    //    }else{
    //
    //        NSLog(@"%@", [formatter stringFromDate:date]);
    //        //昨天
    //        if ([date isYesterday]) {
    //
    //            formatter.dateFormat = @"昨天 HH:mm";
    //            return [formatter stringFromDate:date];
    //
    //            //一周内 [date weekdayStringFromDate]
    //        }else if ([date isSameWeek]){
    //
    //            formatter.dateFormat = [NSString stringWithFormat:@"%@ %@",[date weekdayStringFromDate],@"HH:mm"];
    //            return [formatter stringFromDate:date];
    //
    //            //直接显示年月日
    //        }else{
    //
    //            formatter.dateFormat = @"yy-MM-dd  HH:mm";
    //            return [formatter stringFromDate:date];
    //        }
    //    }
    //    return nil;
}

//时间戳转时间 年月日
+ (NSString *)timeWithTimestamp:(NSInteger )timestamp format:(NSString *)format {
    NSDate *date = [NSDate dateWithTimeIntervalSince1970:timestamp]; //此处根据项目需求,选择是否除以1000 , 如果时间戳精确到秒则去掉1000
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    formatter.dateFormat = format;
    return [formatter stringFromDate:date];
}

+(NSInteger)timeSwitchTimestamp:(NSString *)formatTime andFormatter:(NSString *)format{
    
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateStyle:NSDateFormatterMediumStyle];
    [formatter setTimeStyle:NSDateFormatterShortStyle];
    [formatter setDateFormat:format]; //(@"YYYY-MM-dd hh:mm:ss") ----------设置你想要的格式,hh与HH的区别:分别表示12小时制,24小时制
    
    NSTimeZone* timeZone = [NSTimeZone timeZoneWithName:@"Asia/Beijing"];
    [formatter setTimeZone:timeZone];
    
    NSDate* date = [formatter dateFromString:formatTime]; //------------将字符串按formatter转成nsdate
    //时间转时间戳的方法:
    
    NSInteger timeSp = [[NSNumber numberWithDouble:[date timeIntervalSince1970]] integerValue];
    
    return timeSp;
    
}

// 当前时间
+ (NSString *)getCurrentTimeyyyymmdd {
    
    NSDate *now = [NSDate date];
    NSDateFormatter *formatDay = [[NSDateFormatter alloc] init];
    formatDay.dateFormat = @"yyyy-MM-dd";
    NSString *dayStr = [formatDay stringFromDate:now];
    
    return dayStr;
}

/// 计算时间戳之间的时间差
+ (NSDateComponents *)getDateDifferenceWithBeginTimestamp:(NSString*)beginTimestamp
                                    endTimestamp:(NSString*)endTimestamp {
    
    NSTimeInterval timer1 = [beginTimestamp doubleValue];
    NSTimeInterval timer2 = [endTimestamp doubleValue];
    
    if (timer1 == 0 || timer2 ==0) {
        return 0;
    }
    
    if (beginTimestamp.length >= 13) {
        timer1 = timer1 / 1000;
    }
    if (endTimestamp.length >= 13) {
        timer2 = timer2 / 1000;
    }
    
    NSDateFormatter* formatter = [[NSDateFormatter alloc] init];
    [formatter setDateStyle:NSDateFormatterMediumStyle];
    [formatter setTimeStyle:NSDateFormatterShortStyle];
    [formatter setDateFormat:@"YYYY-MM-dd HH:mm:ss"];
        
    NSDate* date = [NSDate dateWithTimeIntervalSince1970:timer1];
    
    NSDate *date2 = [NSDate dateWithTimeIntervalSince1970:timer2];
        
    // 日历对象(方便比较两个日期之间的差距)
    NSCalendar *calendar = [NSCalendar currentCalendar];
    NSCalendarUnit unit =NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay | NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond;
    NSDateComponents *cmps = [calendar components:unit fromDate:date toDate:date2 options:0];
    
//    NSString *dateString1 = [formatter stringFromDate:date];
//    NSString *dateString2 = [formatter stringFromDate:date2];
//    NSLog(@"%@",dateString1);
//    NSLog(@"%@",dateString2);
//    // 获得某个时间的年月日时分秒
//    NSLog(@"差值%ld月,%ld天,%ld小时%ld分%ld秒", cmps.month,cmps.day ,cmps.hour, cmps.minute,cmps.second);
        
    return cmps;
}
输出结果:
2021-01-15 10:25:02
2021-08-19 20:56:17
差值7月,4天,10小时31分15秒

// 倒计时:天时分秒
+ (NSString *)downTimeWithTimeInterval:(NSInteger)timeInterval {
    
    NSInteger days = (int)(timeInterval/(3600*24));
    NSInteger hours = (int)((timeInterval-days*24*3600)/3600);
    NSInteger minute = (int)(timeInterval-days*24*3600-hours*3600)/60;
    NSInteger second = timeInterval-days*24*3600-hours*3600-minute*60;
    
    NSString *downTimeString = @"";
    if (days == 0) {
        downTimeString = @"";
    }
    if (days > 0) {
        downTimeString = [NSString stringWithFormat:@"%ld天%ld时%ld分%ld秒", days, hours, minute, second];
    } else {
        if (hours > 0) {
            if (hours < 10) {
                downTimeString = [NSString stringWithFormat:@"%0ld时%ld分%ld秒", hours, minute, second];
            } else {
                downTimeString = [NSString stringWithFormat:@"%ld时%ld分%ld秒", hours, minute, second];
            }
        } else {
            if (minute > 0) {
                if (minute < 10) {
                    downTimeString = [NSString stringWithFormat:@"%0ld分%ld秒", minute, second];
                } else {
                    downTimeString = [NSString stringWithFormat:@"%ld分%ld秒", minute, second];
                }
            } else {
                if (second < 10) {
                    downTimeString = [NSString stringWithFormat:@"%0ld秒", second];
                } else if (second == 0) {
                    downTimeString = @"";
                } else {
                    downTimeString = [NSString stringWithFormat:@"%ld秒", second];
                }
            }
        }
    }
    return downTimeString;
}

相关文章

网友评论

      本文标题:【iOS】常见NSDate与时间戳转换

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