iOS: 格式化新浪微博/QQ说说等等的发布时间

本文介绍了如何在社交应用中实现发布时间的格式化显示,包括将特定格式的时间字符串转换为易于理解的时间描述,如“刚刚”、“几分钟前”等,并提供了两个实际案例的代码示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

介绍:对于一些社交工具,我们可以发布一些说说或者心情什么的,如新浪微博,QQ,微信等,发布成功后,上面都会有一个发布的时间。

这个时间并不是具体的NSDate类型,而是经过格式化过的符合一般标准的模式,例如:发布于前一个月、前一个星期、前一天、十几分钟前、刚刚等。

下面就给出两个具体的测试Demo

头文件:

//  ViewController.m
//  测试发布时间格式化
//
//  Created by mac on 16/1/26.
//  Copyright © 2016年 mac. All rights reserved.
//

#import "ViewController.h"

#define knewsTimeFormat @"yyyyMMddHHmmss" //你要传过来日期的格式

#define kcreatedDateFormat @"EEE MMM dd HH:mm:ss Z yyyy" //你要传过来日期的格式

#define kLocaleIdentifier @"en_US"  //时区类型

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];

     注意:传入的需要格式化的时间字符串必须与你设置的日期的格式对应
//测试一
    NSString *str = @"20160126132609";  //  2016/01/26 13:26:09
    NSLog(@"%@",[self newsTime:str]);
    
    
    //测试二
    NSString *str2 = @"Tue Jan 26 13:50:08 +0800 2016";
    NSLog(@"%@",[self formatCreatedDate:str2]);
}

测试一:

//方式一: 获取发布时间
- (NSString *)newsTime:(NSString *)newsTimes
{
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    formatter.dateFormat = knewsTimeFormat;
    formatter.locale = [[NSLocale alloc] initWithLocaleIdentifier:kLocaleIdentifier];
    
    NSDate *date = [formatter dateFromString:newsTimes];
    
    NSDate *now = [NSDate date];
    
    // 比较帖子发布时间和当前时间
    NSTimeInterval interval = [now timeIntervalSinceDate:date];
    
    NSString *format;
    if (interval <= 60) {
        format = @"刚刚";
    } else if(interval <= 60*60){
        format = [NSString stringWithFormat:@"发布于前%.f分钟", interval/60];
    } else if(interval <= 60*60*24){
        format = [NSString stringWithFormat:@"发布于前%.f小时", interval/3600];
    } else if (interval <= 60*60*24*7){
        format = [NSString stringWithFormat:@"发布于前%d天", (int)interval/(60*60*24)];
    } else if (interval > 60*60*24*7 & interval <= 60*60*24*30 ){
        format = [NSString stringWithFormat:@"发布于前%d周", (int)interval/(60*60*24*7)];
    }else if(interval > 60*60*24*30 ){
        format = [NSString stringWithFormat:@"发布于前%d月", (int)interval/(60*60*24*30)];
    }
    
    formatter.dateFormat = format;
    return [formatter stringFromDate:date];
}

输出结果:

2016-01-26 14:32:28.324 测试发布时间格式化[1809:109947] 发布于前1小时

 

 

 

测试二:

//方式二: 获取发布时间
-(NSString *)formatCreatedDate:(NSString *)newsTimes
{
    NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
    
    formatter.dateFormat = kcreatedDateFormat;
    
    formatter.locale = [[NSLocale alloc]initWithLocaleIdentifier:kLocaleIdentifier];
    
    NSDate *date = [formatter dateFromString:newsTimes];
    
    NSDate *now = [NSDate date];
    
    // 比较帖子发布时间和当前时间
    NSTimeInterval timeInterval =  [now timeIntervalSinceDate:date];
    
    if(timeInterval < 60) //1分钟
    {
        return @"最近";
    }
    else if(timeInterval < 60*60)  //1小时
    {
        return [NSString stringWithFormat:@"%d分钟前",(int)timeInterval/60];
    }
    else if(timeInterval < 60*60*24) //1天
    {
        return [NSString stringWithFormat:@"%d小时前",(int)timeInterval/60/60];
    }
    
    return [NSString stringWithFormat:@"%.1lf",timeInterval];
}
@end

输出结果:

2016-01-26 14:32:28.325 测试发布时间格式化[1809:109947] 42分钟前

 

转载于:https://www.cnblogs.com/XYQ-208910/p/5160381.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值