-
类似朋友圈发布时间显示(刚刚... 多少分钟多少小时前 昨天)
- (NSString *)transformDateStrToMinutes:(NSString *)dateStr {NSDateFormatter *date=[[NSDateFormatter alloc] init]; [date setDateFormat:@"yyyy-MM-dd HH:mm:ss"]; NSDate *d=[date dateFromString:dateStr]; NSTimeInterval late=[d timeIntervalSince1970]*1; //当前时间 NSDate* dat = [NSDate dateWithTimeIntervalSinceNow:0]; NSTimeInterval now=[dat timeIntervalSince1970]*1; //单位是秒 NSTimeInterval cha=now-late; NSString *timeString= @""; if (cha / 3600 < 1) { //小时 timeString = [NSString stringWithFormat:@"%f", cha/60]; // NSLog(@"timeString%@",timeString); timeString = [timeString substringToIndex:timeString.length-7]; int num= [timeString intValue]; if (num <= 1) { timeString = [NSString stringWithFormat:@"刚刚..."]; // NSLog(@"timeString = %@",timeString); }else{ timeString = [NSString stringWithFormat:@"%@分钟前", timeString]; // NSLog(@"timeString = %@",timeString); } } //86400=1天 if (cha / 3600 > 1 && cha / 86400 < 1){ timeString = [NSString stringWithFormat:@"%f", cha/3600]; timeString = [timeString substringToIndex:timeString.length-7]; timeString=[NSString stringWithFormat:@"%@小时前", timeString]; // NSLog(@"timeString = %@",timeString); } if (cha/86400>1){ timeString = [NSString stringWithFormat:@"%f", cha/86400]; timeString = [timeString substringToIndex:timeString.length-7]; int num=[timeString intValue]; if (num<2){ timeString = [NSString stringWithFormat:@"昨天"]; // NSLog(@"timeString = %@",timeString); }else { NSString * secondStr = [dateStr substringFromIndex:5]; timeString = [secondStr substringToIndex:11]; } } return timeString; }
-
时间戳转化为时间NSDate
- (NSString *)timeWithTimeIntervalString:(NSString )timeString
{
// 格式化时间
NSDateFormatter formatter =[[NSDateFormatter alloc] init];
formatter.timeZone = [NSTimeZone timeZoneWithName:@"beijing"];
[formatter setDateStyle:NSDateFormatterMediumStyle];
[formatter setTimeStyle:NSDateFormatterShortStyle];
[formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];// 毫秒值转化为秒 NSDate* date = [NSDate dateWithTimeIntervalSince1970:[timeString doubleValue]/ 1000.0]; NSString* dateString = [formatter stringFromDate:date]; return dateString; }
-
将时间戳转化成具体时间字符串(具体到秒)
- (NSString *)transformTimeStampToTimeStr:(NSString *)timeStamp {
NSString *time = [NSString stringWithFormat:@"%f",[timeStamp doubleValue]];NSInteger num = [time integerValue]/1000; NSDateFormatter *formatter = [[NSDateFormatter alloc] init]; [formatter setDateStyle:NSDateFormatterMediumStyle]; [formatter setTimeStyle:NSDateFormatterShortStyle]; [formatter setDateFormat:@"YYYY-MM-dd HH:mm:ss"]; NSDate * confromTimesp = [NSDate dateWithTimeIntervalSince1970:num]; NSString * confromTimespStr = [formatter stringFromDate:confromTimesp]; return confromTimespStr; }
iOS 与时间相关的处理
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...