ios 两个时间之间的比较,相差多少天多少小时多少分多少秒
来源:互联网 发布:数据库设计说明书实例 编辑:程序博客网 时间:2024/06/10 23:57
两个时间之间的比较,相差多少天多少小时多少分多少秒
// 1. 今年
// 1分钟内:刚刚
// 1分~59分内:xx小时前
// 昨天 xx:xx
// xx-xx xx:xx
// 2. 非今年
// xxxx-xx-xx xx:xx
// Wed Jul 08 10:01:03 +0800 2015
NSDateFormatter *fmt = [[NSDateFormatter alloc] init];
// 如果是真机调试,转换这种欧美时间,需要设置locale
fmt.locale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US"];
fmt.dateFormat = @"EEE MMM dd HH:mm:ss Z yyyy";
NSDate *createDate = [fmt dateFromString:_created_at];
// 当前时间
NSDate *now = [NSDate date];
// 日历对象 (方便比较两个日期之间的差距)
NSCalendar *calendar = [NSCalendar currentCalendar];
// NSCalendarUnit 枚举代表想获得哪些差值
NSCalendarUnit unit = NSCalendarUnitYear | NSCalendarUnitWeekOfMonth | NSCalendarUnitDay | NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond;
// 计算两个日期之间的差值
NSDateComponents *cmps = [calendar components:unit fromDate:createDate toDate:now options:0];
// 获得某个时间的年月日时分秒
NSDateComponents *createDateCmps = [calendar components:unit fromDate:createDate];
NSDateComponents *nowCmps = [calendar components:unit fromDate:now];
if (createDateCmps.year == nowCmps.year) { // 今年
if (cmps.day == 1) { // 昨天
fmt.dateFormat = @"昨天 HH:mm";
return [fmt stringFromDate:createDate];
} else if (cmps.day == 0){ // 今天
if (cmps.hour > 1) { // 大于1小时前
return [NSString stringWithFormat:@"%d小时前", cmps.hour];
}
else if (cmps.minute >= 1) {
return [NSString stringWithFormat:@"%d分钟前", cmps.minute];
}
else {
return @"刚刚";
}
}
else { // 今年的其他日子
fmt.dateFormat = @"MM-dd HH:mm";
return [fmt stringFromDate:createDate];
}
}
else { // 非今年
fmt.dateFormat = @"yyyy-MM-dd HH:mm:ss";
return [fmt stringFromDate:createDate];
}
// 1. 今年
// 1分钟内:刚刚
// 1分~59分内:xx小时前
// 昨天 xx:xx
// xx-xx xx:xx
// 2. 非今年
// xxxx-xx-xx xx:xx
// Wed Jul 08 10:01:03 +0800 2015
NSDateFormatter *fmt = [[NSDateFormatter alloc] init];
// 如果是真机调试,转换这种欧美时间,需要设置locale
fmt.locale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US"];
fmt.dateFormat = @"EEE MMM dd HH:mm:ss Z yyyy";
NSDate *createDate = [fmt dateFromString:_created_at];
// 当前时间
NSDate *now = [NSDate date];
// 日历对象 (方便比较两个日期之间的差距)
NSCalendar *calendar = [NSCalendar currentCalendar];
// NSCalendarUnit 枚举代表想获得哪些差值
NSCalendarUnit unit = NSCalendarUnitYear | NSCalendarUnitWeekOfMonth | NSCalendarUnitDay | NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond;
// 计算两个日期之间的差值
NSDateComponents *cmps = [calendar components:unit fromDate:createDate toDate:now options:0];
// 获得某个时间的年月日时分秒
NSDateComponents *createDateCmps = [calendar components:unit fromDate:createDate];
NSDateComponents *nowCmps = [calendar components:unit fromDate:now];
if (createDateCmps.year == nowCmps.year) { // 今年
if (cmps.day == 1) { // 昨天
fmt.dateFormat = @"昨天 HH:mm";
return [fmt stringFromDate:createDate];
} else if (cmps.day == 0){ // 今天
if (cmps.hour > 1) { // 大于1小时前
return [NSString stringWithFormat:@"%d小时前", cmps.hour];
}
else if (cmps.minute >= 1) {
return [NSString stringWithFormat:@"%d分钟前", cmps.minute];
}
else {
return @"刚刚";
}
}
else { // 今年的其他日子
fmt.dateFormat = @"MM-dd HH:mm";
return [fmt stringFromDate:createDate];
}
}
else { // 非今年
fmt.dateFormat = @"yyyy-MM-dd HH:mm:ss";
return [fmt stringFromDate:createDate];
}
0 0
- ios 两个时间之间的比较,相差多少天多少小时多少分多少秒
- 判断两个时间相差多少天多少分多少秒
- java计算两个日期间相差多少天多少小时多少分多少秒
- 计算格式为YYYY-MM-DD HH:ii:ss的两个时间,相差多少年、多少月、多少日、多少小时、多少分钟、多少秒?
- 两个时间之间 相差多少
- java判断2个时间相差多少天、多少小时、多少分
- java判断2个时间相差多少天、多少小时、多少分
- SQL Server返回两个Date日期相差共多少天零多少小时零多少分钟零多少秒
- 反回两个时间相差多少小时
- GoLang 获取两个时间相差多少小时
- 两个时间相差多少天
- 计算两个时间 相差多少时分秒
- JAVA 时间差距,两个时间相差多少天,时,分,秒
- JAVA 时间差距,两个时间相差多少天,时,分,秒
- JAVA 时间差距,两个时间相差多少天,时,分,秒
- JAVA 时间差距,两个时间相差多少天,时,分,秒
- JAVA 时间差距,两个时间相差多少天,时,分,秒
- 距离2012还有多少秒?多少分?多少小时?
- 【矩阵分解】Python下基于Numpy的四种矩阵基本分解的实现
- 数据结构与算法(序)
- Java学习路线图
- c++ primer第五章练习题总结
- 【C++竞赛 G】Lines
- ios 两个时间之间的比较,相差多少天多少小时多少分多少秒
- 继承
- 单链表的实现。
- 数据库索引
- 第十三周项目四-Floyd算法验证
- Python3 C扩展
- linux压缩或解压缩命令
- 《机器学习实战》——决策树代码
- (二)Android socket+多线程 实现聊天功能