實(shí)際開發(fā)中項(xiàng)目中有涉及到日期,服務(wù)返回的都是時(shí)間戳的字符串巨坊,長度為10或者13撬槽,在客戶端需要進(jìn)行處理轉(zhuǎn)換為格式時(shí)間。
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
//服務(wù)器返回的字符串趾撵,此處為10位字符串
NSString *time = @"1495641600";
NSLog(@"%@",[self ConvertStrToTime:time]);
}
//時(shí)間戳變?yōu)楦袷綍r(shí)間
- (NSString *)ConvertStrToTime:(NSString *)timeStr
{
long long time=[timeStr longLongValue];
// 如果服務(wù)器返回的是13位字符串侄柔,需要除以1000,否則顯示不正確(13位其實(shí)代表的是毫秒占调,需要除以1000)
// long long time=[timeStr longLongValue] / 1000;
NSDate *date = [[NSDate alloc]initWithTimeIntervalSince1970:time];
NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
[formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
NSString*timeString=[formatter stringFromDate:date];
return timeString;
}
打印結(jié)果為 2017-05-25 00:00:00