Translate

bloggerads內文

2017年4月13日 星期四

IOS NSDate 毫秒問題

之前遇到json 資料是毫秒時候,解法如以下範例.

例:
日期格式:/Date(1490779686507)/



 if (publishTimeBeg != [NSNull null]) {
        NSString *timestamp = [[publishTimeBeg 
componentsSeparatedByString:@"("][1] 
componentsSeparatedByString:@")"][0];
       
        NSDate *date = [NSDate dateWithTimeIntervalSince1970:[timestamp integerValue]/1000];
        NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
        [dateFormatter setLocale:[[NSLocale alloc]initWithLocaleIdentifier:@"cn_TW"]];
        [dateFormatter setDateFormat:@"yyyy-MM-dd"];
        dateStr = [dateFormatter stringFromDate:date];


    }

沒有留言:

張貼留言