- (NSString *)preettyTime:(long long)ts
{
//原有时间
NSString *firstDateStr=[self FormatTime:@"yyyy-MM-dd" timeInterval:ts];
NSArray *firstDateStrArr=[firstDateStr componentsSeparatedByString:@"-"];
//现在时间
NSDate *now = [NSDate date];
NSDateComponents *componentsNow = [[NSCalendar currentCalendar] components:NSDayCalendarUnit | NSMonthCalendarUnit | NSYearCalendarUnit fromDate:now];
NSString *nowDateStr = [NSString stringWithFormat:@"%ld-%ld-%ld", (long)[componentsNow year], (long)[componentsNow month], (long)[componentsNow day]];
NSArray *nowDateStrArr = [nowDateStr componentsSeparatedByString:@"-"];
NSDate *date = [NSDate dateWithTimeIntervalSince1970:ts/1000];
NSDateFormatter *dateformatter = [[NSDateFormatter alloc] init];
//同年
if ([firstDateStrArr[0] intValue] == [nowDateStrArr[0] intValue]){
//当天
if( [firstDateStrArr[1] intValue] == [nowDateStrArr[1] intValue] && [nowDateStrArr[2] intValue]== [firstDateStrArr[2] intValue]){
[dateformatter setDateFormat:@"HH:mm"];
return [NSString stringWithFormat:@"%@", [dateformatter stringFromDate:date]];
}
//昨天
if( [firstDateStrArr[1] intValue]==[nowDateStrArr[1] intValue] && ([nowDateStrArr[2] intValue]-[firstDateStrArr[2] intValue]==1)){
[dateformatter setDateFormat:@"HH:mm"];
return [NSString stringWithFormat:@"昨天 %@", [dateformatter stringFromDate:date]];
}else{//昨天之前
[dateformatter setDateFormat:@"MM-dd HH:mm"];
return [dateformatter stringFromDate:date];
}
}else{
[dateformatter setDateFormat:@"yyyy-MM-dd HH:mm"];
return [dateformatter stringFromDate:date];
}
}
- (NSString *)FormatTime:(NSString *)format timeInterval:(double)value;
{
NSDateFormatter *dateformatter = [[NSDateFormatter alloc] init];
NSDate *date = [NSDate dateWithTimeIntervalSince1970:value / 1000];
[dateformatter setDateFormat:format];
return [dateformatter stringFromDate:date];
}
获取两个时间的间隔
NSDateFormatter *dateFomatter = [[NSDateFormatter alloc] init];
dateFomatter.dateFormat = @"yyyy-MM-dd HH:mm:ss";
// 截止时间字符串格式
NSString *expireDateStr = status.expireDatetime;
// 当前时间字符串格式
NSString *nowDateStr = [dateFomatter stringFromDate:nowDate];
// 截止时间data格式
NSDate *expireDate = [dateFomatter dateFromString:expireDateStr];
// 当前时间data格式
nowDate = [dateFomatter dateFromString:nowDateStr];
// 当前日历
NSCalendar *calendar = [NSCalendar currentCalendar];
// 需要对比的时间数据
NSCalendarUnit unit = NSCalendarUnitYear | NSCalendarUnitMonth
| NSCalendarUnitDay | NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond;
// 对比时间差
NSDateComponents *dateCom = [calendar components:unit fromDate:nowDate toDate:expireDate options:0];
年差额 = dateCom.year, 月差额 = dateCom.month, 日差额 = dateCom.day, 小时差额 = dateCom.hour, 分钟差额 = dateCom.minute, 秒差额 = dateCom.second
判断当前时间是否过期
NSDate *nowDate = [NSDate date];
NSDateFormatter *dateFomatter = [[NSDateFormatter alloc] init];
dateFomatter.dateFormat = @"yyyy-MM-dd HH:mm:ss.0";
// 截止时间字符串格式
NSString *expireDateStr = model.endtime;
// 当前时间字符串格式
NSString *nowDateStr = [dateFomatter stringFromDate:nowDate];
// 截止时间data格式
NSDate *expireDate = [dateFomatter dateFromString:expireDateStr];
// 当前时间data格式
nowDate = [dateFomatter dateFromString:nowDateStr];
int status = [self compareOneDay:nowDate withAnotherDay:expireDate];
- (int)compareOneDay:(NSDate *)oneDay withAnotherDay:(NSDate *)anotherDay
{
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"yyyy-MM-dd hh:mm:ss"];
NSString *oneDayStr = [dateFormatter stringFromDate:oneDay];
NSString *anotherDayStr = [dateFormatter stringFromDate:anotherDay];
NSDate *dateA = [dateFormatter dateFromString:oneDayStr];
NSDate *dateB = [dateFormatter dateFromString:anotherDayStr];
NSComparisonResult result = [dateA compare:dateB];
NSLog(@"oneDay : %@, anotherDay : %@", oneDay, anotherDay);
if (result == NSOrderedDescending) {
//在指定时间前面 过了指定时间 过期
NSLog(@"oneDay is in the future");
return 1;
}
else if (result == NSOrderedAscending){
//没过指定时间 没过期
//NSLog(@"Date1 is in the past");
return -1;
}
//刚好时间一样.
//NSLog(@"Both dates are the same");
return 0;
}
网友评论