让时间不再随系统设置而改变

为 NSString 写了一个类别, 把 时间戳 输出时间格式

 1 + (NSString *)timeByTimestamp:(NSTimeInterval)timestamp {
 2     
 3     
 4     NSTimeZone *zone = [NSTimeZone defaultTimeZone];
 5     
 6     static NSDateFormatter *formatter =nil;
 7     static dispatch_once_t onceToken;
 8     dispatch_once(&onceToken, ^{
 9         formatter = [[NSDateFormatter alloc] init];
10     });
11     formatter.locale=[[NSLocale alloc]initWithLocaleIdentifier:@"en_US"];
12     [formatter setDateFormat:@"yyyy-MM-dd aa hh:mm"];
13     [formatter setTimeZone:zone];
14     
15     CGFloat thatTime = timestamp;
16     NSDate *thatDate = [NSDate dateWithTimeIntervalSince1970:thatTime];
17     NSString *thatString = [formatter stringFromDate:thatDate];
18     NSDate *nowDate = [NSDate date];
19     CGFloat nowTime = [nowDate timeIntervalSince1970];
20     
21     CGFloat spaceTime = nowTime - thatTime;
22     
23     if (spaceTime / 60 < 5) {
24         return @"刚刚";
25     } else if (spaceTime < 60 * 60 * 24) {
26         NSString *time = [thatString substringFromIndex:11];
27         if ([time hasPrefix:@"PM"]) {
28             time = [time stringByReplacingOccurrencesOfString:@"PM" withString:@"下午"];
29         }
30         if ([time hasPrefix:@"AM"]) {
31             time = [time stringByReplacingOccurrencesOfString:@"AM" withString:@"上午"];
32         }
33         return time;
34     } else if (spaceTime < 60 * 60 * 24 * 2) {
35         NSString *time = [thatString substringFromIndex:11];
36         if ([time hasPrefix:@"PM"]) {
37             time = [time stringByReplacingOccurrencesOfString:@"PM" withString:@"昨天下午"];
38         }
39         if ([time hasPrefix:@"AM"]) {
40             time = [time stringByReplacingOccurrencesOfString:@"AM" withString:@"昨天上午"];
41         }
42         return time;
43     } else if (spaceTime < 60 * 60 * 24 * 3) {
44         NSString *time = [thatString substringFromIndex:11];
45         if ([time hasPrefix:@"PM"]) {
46             time = [time stringByReplacingOccurrencesOfString:@"PM" withString:@"前天下午"];
47         }
48         if ([time hasPrefix:@"AM"]) {
49             time = [time stringByReplacingOccurrencesOfString:@"AM" withString:@"前天上午"];
50         }
51         return time;
52     } else if (spaceTime < 60 * 60 * 24 * 4) {
53         NSString *time = [thatString substringFromIndex:11];
54         if ([time hasPrefix:@"PM"]) {
55             time = [time stringByReplacingOccurrencesOfString:@"PM" withString:@"大前天下午"];
56         }
57         if ([time hasPrefix:@"AM"]) {
58             time = [time stringByReplacingOccurrencesOfString:@"AM" withString:@"大前天上午"];
59         }
60         return time;
61     } else if (spaceTime < 60 * 60 * 24 * 365) {
62         NSDateFormatter *dateFor = [[NSDateFormatter alloc] init];
63         [dateFor setDateFormat:@"M月d日 aa h:mm"];
64         [dateFor setTimeZone:zone];
65         NSString *time = [dateFor stringFromDate:thatDate];
66         time = [time stringByReplacingOccurrencesOfString:@"PM" withString:@"下午"];
67         time = [time stringByReplacingOccurrencesOfString:@"AM" withString:@"上午"];
68         return time;
69     } else {
70         NSDateFormatter *dateFor = [[NSDateFormatter alloc] init];
71         [dateFor setDateFormat:@"yyyy年M月d日 aa h:mm"];
72         [dateFor setTimeZone:zone];
73         NSString *time = [dateFor stringFromDate:thatDate];
74         time = [time stringByReplacingOccurrencesOfString:@"PM" withString:@"下午"];
75         time = [time stringByReplacingOccurrencesOfString:@"AM" withString:@"上午"];
76         return time;
77     }
78 }

 

转载于:https://www.cnblogs.com/shenhongbang/p/4547558.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值