iOS 日期格式转几年几月几日几时几分几秒前
来源:互联网 发布:nba弹跳体测数据 编辑:程序博客网 时间:2024/04/30 07:57
+ (NSString *)prettyDateWithReferences:(NSString *)dateString {
//2015-09-28T11:00:17+08:00 原来字符串时间
NSString *year1 = [dateString substringToIndex:10];
NSString *mon1 = [dateString substringWithRange:NSMakeRange(11, 8)];
NSString *str = [NSString stringWithFormat:@"%@ %@",year1,mon1];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
NSDate *reference = [dateFormatter dateFromString:str];
NSString *suffix = @"后";
NSDate *date = [NSDate date];
float different = [reference timeIntervalSinceDate:date];
if (different < 0) {
different = -different;
suffix = @"前";
}
// days = different / (24 * 60 * 60), take the floor value
float dayDifferent = floor(different / 86400);
int days = (int)dayDifferent;
int weeks = (int)ceil(dayDifferent / 7);
int months = (int)ceil(dayDifferent / 30);
int years = (int)ceil(dayDifferent / 365);
// It belongs to today
if (dayDifferent <= 0) {
// lower than 60 seconds
if (different < 60) {
return @"刚刚";
}
// lower than 120 seconds => one minute and lower than 60 seconds
if (different < 120) {
return [NSString stringWithFormat:@"1分钟%@", suffix];
}
// lower than 60 minutes
if (different < 660 * 60) {
return [NSString stringWithFormat:@"%d分钟%@", (int)floor(different / 60), suffix];
}
// lower than 60 * 2 minutes => one hour and lower than 60 minutes
if (different < 7200) {
return [NSString stringWithFormat:@"1小时%@",suffix];
}
// lower than one day
if (different < 86400) {
return [NSString stringWithFormat:@"%d小时%@", (int)floor(different / 3600), suffix];
}
}
// lower than one week
else if (days < 7) {
return [NSString stringWithFormat:@"%d天%@%@", days, days == 1 ? @"" : @"", suffix];
}
// lager than one week but lower than a month
else if (weeks < 4) {
return [NSString stringWithFormat:@"%d星期%@%@", weeks, weeks == 1 ? @"" : @"", suffix];
}
// lager than a month and lower than a year
else if (months < 12) {
return [NSString stringWithFormat:@"%d月%@%@", months, months == 1 ? @"" : @"", suffix];
}
// lager than a year
else {
return [NSString stringWithFormat:@"%d年%@%@", years, years == 1 ? @"" : @"", suffix];
}
return self.description;
}
//2015-09-28T11:00:17+08:00 原来字符串时间
NSString *year1 = [dateString substringToIndex:10];
NSString *mon1 = [dateString substringWithRange:NSMakeRange(11, 8)];
NSString *str = [NSString stringWithFormat:@"%@ %@",year1,mon1];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
NSDate *reference = [dateFormatter dateFromString:str];
NSString *suffix = @"后";
NSDate *date = [NSDate date];
float different = [reference timeIntervalSinceDate:date];
if (different < 0) {
different = -different;
suffix = @"前";
}
// days = different / (24 * 60 * 60), take the floor value
float dayDifferent = floor(different / 86400);
int days = (int)dayDifferent;
int weeks = (int)ceil(dayDifferent / 7);
int months = (int)ceil(dayDifferent / 30);
int years = (int)ceil(dayDifferent / 365);
// It belongs to today
if (dayDifferent <= 0) {
// lower than 60 seconds
if (different < 60) {
return @"刚刚";
}
// lower than 120 seconds => one minute and lower than 60 seconds
if (different < 120) {
return [NSString stringWithFormat:@"1分钟%@", suffix];
}
// lower than 60 minutes
if (different < 660 * 60) {
return [NSString stringWithFormat:@"%d分钟%@", (int)floor(different / 60), suffix];
}
// lower than 60 * 2 minutes => one hour and lower than 60 minutes
if (different < 7200) {
return [NSString stringWithFormat:@"1小时%@",suffix];
}
// lower than one day
if (different < 86400) {
return [NSString stringWithFormat:@"%d小时%@", (int)floor(different / 3600), suffix];
}
}
// lower than one week
else if (days < 7) {
return [NSString stringWithFormat:@"%d天%@%@", days, days == 1 ? @"" : @"", suffix];
}
// lager than one week but lower than a month
else if (weeks < 4) {
return [NSString stringWithFormat:@"%d星期%@%@", weeks, weeks == 1 ? @"" : @"", suffix];
}
// lager than a month and lower than a year
else if (months < 12) {
return [NSString stringWithFormat:@"%d月%@%@", months, months == 1 ? @"" : @"", suffix];
}
// lager than a year
else {
return [NSString stringWithFormat:@"%d年%@%@", years, years == 1 ? @"" : @"", suffix];
}
return self.description;
}
0 0
- iOS 日期格式转几年几月几日几时几分几秒前
- 用十进制减法的思想,算出两个unix时间戳格式时间之间差几年几月几日几时几分几秒
- Android针对时间戳转几时几分昨天前天某年某月某日的显示
- 根据秒数写一个倒计时,还有几时几分几秒开始
- 根据秒数转换剩余几天几时几分
- java计算两个日期之间的时间间隔,显示格式为几年几月几日
- iOS 日期转换格式(字符串转日期格式)
- ios 日期格式 日期转换
- iOS 日期格式 日期转换
- ios 日期格式 日期转换
- 计算两个日期之间相差几年几月几日。
- iOS 日期与字符串格式互转
- iOS 日期 格式 转换
- IOS关于日期格式
- ios NSDateFormatter日期格式
- ios日期格式转换
- iOS 日期 格式 转换
- ios日期格式转换
- 23种设计模式(2):工厂方法模式
- STM32F4学习笔记2——时钟与复位系统
- 大学档案处理
- 关于c加加的一点思考
- STL学习_List篇
- iOS 日期格式转几年几月几日几时几分几秒前
- Dom4j读取xml文档
- hibernate中 saveorupdate(),save(),update(),merge()你是怎么看待的;
- 自己写一个js表单验证框架(转)
- Linux操作系统普及
- java.lang.OutOfMemoryError: PermGen space
- 使用Ajax的异步登陆
- poj1144Network 无向图求割点Tarjan
- 动态规划(最长上升子序列)