首页 技术 正文
技术 2022年11月21日
0 收藏 776 点赞 3,285 浏览 1432 个字

 iOS-OC根据时间戳获取距离现在的状态(刚刚,分钟前,今天,昨天)

获取时间戳

  1. – (NSString *)distanceTimeWithBeforeTime:(double)beTime
  2. {
  3. NSTimeInterval now = [[NSDatedate]timeIntervalSince1970];
  4. double distanceTime = now – beTime;
  5. NSString * distanceStr;
  6. NSDate * beDate = [NSDatedateWithTimeIntervalSince1970:beTime];
  7. NSDateFormatter * df = [[NSDateFormatteralloc]init];
  8. [df setDateFormat:@”HH:mm”];
  9. NSString * timeStr = [df stringFromDate:beDate];
  10. [df setDateFormat:@”dd”];
  11. NSString * nowDay = [df stringFromDate:[NSDate date]];
  12. NSString * lastDay = [df stringFromDate:beDate];
  13. if (distanceTime < 60) {//小于一分钟
  14. distanceStr = @”刚刚”;
  15. }
  16. else if (distanceTime <60*60) {//时间小于一个小时
  17. distanceStr = [NSString stringWithFormat:@”%ld分钟前”,(long)distanceTime/60];
  18. }
  19. else if(distanceTime <24*60*60 && [nowDayintegerValue] == [lastDay integerValue]){//时间小于一天
  20. distanceStr = [NSString stringWithFormat:@”今天 %@”,timeStr];
  21. }
  22. else if(distanceTime<24*60*60*2 && [nowDayintegerValue] != [lastDay integerValue]){
  23. if ([nowDay integerValue] – [lastDay integerValue] ==1 || ([lastDay integerValue] – [nowDayintegerValue] > 10 && [nowDayintegerValue] == 1)) {
  24. distanceStr = [NSString stringWithFormat:@”昨天 %@”,timeStr];
  25. }
  26. else{
  27. [df setDateFormat:@”MM-dd HH:mm”];
  28. distanceStr = [df stringFromDate:beDate];
  29. }
  30. }
  31. else if(distanceTime <24*60*60*365){
  32. [df setDateFormat:@”MM-dd HH:mm”];
  33. distanceStr = [df stringFromDate:beDate];
  34. }
  35. else{
  36. [df setDateFormat:@”yyyy-MM-dd HH:mm”];
  37. distanceStr = [df stringFromDate:beDate];
  38. }
  39. return distanceStr;
  40. }
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,000
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,512
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,358
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,141
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,771
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,849