做项目时,后台给返回的数据是 毫秒
这样就需要转换时间搓 变成我们需要的样式,如:2016年8月30日 11:18
OC:
swift:
这样就会转换成我们想要的样式。
1.把获取的时间转换成当前时间
NSDate *dateNow = [NSDate date];
NSTimeZone *zone = [NSTimeZone systemTimeZone];
NSInteger interval = [zone secondsFromGMTForDate:dateNow];
NSDate *localDate = [dateNow dateByAddingTimeInterval:interval];
NSLog(@"%@",localDate);
2.把当前时间转换成时间戳
_timeSp = (long)[dateNow timeIntervalSince1970];
NSLog(@"timeSp:%ld",(long)_timeSp); //时间戳的值
3.把时间戳转换为时间
NSDate *time = [NSDate dateWithTimeIntervalSinceNow:1464274800];
NSLog(@"%@",time);
4.输出只有年月日 忽略时分秒
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"yyyy年MM月dd日"];
NSString *str = [formatter stringFromDate:date];