项目中 UTC时间字符串 转换 本地时间 的过程 ;以及获取年月日

  • 服务端接口下发的时间字符串 是UTC时间,但app显示的时候肯定需要进行转换成本地时区(中国北京)的时间字符串,以下是我进行处理的过程:
    NSDictionary *dic = @{@"create_time":@"2017-10-25 02:07:39"};
    
    NSDateFormatter *format = [[NSDateFormatter alloc] init];
    format.dateFormat = @"yyyy-MM-dd HH:mm:ss";
    NSTimeZone *sourceTimeZone = [NSTimeZone timeZoneWithName:@"UTC"];
    //NSTimeZone *sourceTimeZone = [NSTimeZone timeZoneWithAbbreviation:@"UTC"];
    format.timeZone = sourceTimeZone;
    
    NSDate *createDate = [format dateFromString:dic[@"create_time"]];
    NSTimeZone *desTimeZone = [NSTimeZone localTimeZone];
    format.timeZone = desTimeZone;
    NSString *localStr = [format stringFromDate:createDate];
    NSLog(@"%@", localStr); //2017-10-25 10:07:39
  • 为了获取年月日进行了一下操作
    NSCharacterSet *charSet = [NSCharacterSet characterSetWithCharactersInString:@" -:"];
    NSArray *createTimeArr = [localStr componentsSeparatedByCharactersInSet:charSet];
  • 为了防止createTimeArr直接取值时发生异常,将createTimeArr存放在一个TimeFormatModel类中,类的定义如下:
@interface LTTimeFormatModel : NSObject
@property (nonatomic, strong) NSString *year;
@property (nonatomic, strong) NSString *month;
@property (nonatomic, strong) NSString *day;
@property (nonatomic, strong) NSString *hour;
@property (nonatomic, strong) NSString *minute;
@property (nonatomic, strong) NSString *second;
@property (nonatomic, strong) NSString *timeStr;
- (instancetype)initWithArray:(NSArray *)arr timeStr:(NSString *)timeStr;
@end

@implementation LTTimeFormatModel
- (instancetype)initWithArray:(NSArray *)arr timeStr:(NSString *)timeStr{
    if (self = [super init]) {
        if (arr && arr.count == 6) {
            self.year = arr[0];
            self.month = arr[1];
            self.day = arr[2];
            self.hour = arr[3];
            self.minute = arr[4];
            self.second = arr[5];
        }
        self.timeStr = timeStr;
    }
    return self;
}
@end
  • 获取年月日还可以通过一下两个方法
    //1.NSDateFormat获取
    NSDate *date =[NSDate date];
    NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
    [formatter setDateFormat:@"yyyy"];
    NSInteger currentYear=[[formatter stringFromDate:date] integerValue];
    [formatter setDateFormat:@"MM"];
    NSInteger currentMonth=[[formatter stringFromDate:date]integerValue];
    [formatter setDateFormat:@"dd"];
    NSInteger currentDay=[[formatter stringFromDate:date] integerValue];
    
    NSLog(@"currentDate = %@ ,year = %ld ,month=%ld, day=%ld",date,currentYear,currentMonth,currentDay);
    
    //2.NSDateComponents获取
    NSDate  *currentDate = [NSDate date];
    NSCalendar *calendar = [NSCalendar currentCalendar];
    NSDateComponents *components = [calendar components:NSCalendarUnitYear|NSCalendarUnitMonth|NSCalendarUnitDay fromDate:currentDate];
    
    NSInteger year=[components year];
    NSInteger month=[components month];
    NSInteger day=[components day];
    NSLog(@"currentDate = %@ ,year = %ld ,month=%ld, day=%ld",currentDate,year,month,day);

你可能感兴趣的:(项目中 UTC时间字符串 转换 本地时间 的过程 ;以及获取年月日)