我有一个以毫秒为单位的日期时间,例如1347616929,我想转换它以“dd / MM / yyyy hh:mm:ss”格式获取日期和时间。
答案 0 :(得分:3)
首先,你必须通过将毫秒除以1000来将毫秒转换为秒。
NSTimeInterval timeInSecond = 1347616929/1000;
NSDate *date = [NSDate dateWithTimeIntervalSince1970:timeInSecond];
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"dd/MM/yyyy HH:mm:ss"];
NSString *formattedDate=[formatter stringFromDate:date];
NSLog(@"Formatted Date : %@",formattedDate);
答案 1 :(得分:1)
如果它真的是毫秒,你可以使用像
这样的东西NSTimeInterval timeInterval = 1347616929/1000;
NSDate *date = [NSDate dateWithTimeIntervalSince1970:timeInterval];
如果要保持毫秒数,则还必须向NSDate添加1347616929%1000
毫秒。
偏移真的看起来像秒,这意味着你可以立即使用它作为秒;
NSDate *date = [NSDate dateWithTimeIntervalSince1970:1347616929];
转换后,您可以使用普通的NSDateFormatter函数对其进行格式化,以根据需要对其进行格式化;
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"dd'/'MM'/'yyyy' 'hh':'mm':'ss'.'SSS"];
NSString *formattedDateString = [dateFormatter stringFromDate:date];