zoukankan      html  css  js  c++  java
  • 让时间不再随系统设置而改变

    为 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 }
  • 相关阅读:
    洛谷P2045 K方格取数(算竞进阶习题)
    洛谷P2764 最小路径覆盖问题
    BZOJ 1051 受欢迎的牛
    BZOJ 4196 软件包管理器
    跨域知识(一)——CORS
    CSS 实现隐藏滚动条同时又可以滚动
    数组map用法总结
    js和Jquery获取选中select值和文本
    closest和parents方法区别
    CSS面试题总结2(转)
  • 原文地址:https://www.cnblogs.com/shenhongbang/p/4547558.html
Copyright © 2011-2022 走看看