我想将时间戳值1308031456
转换为NSDate
格式(在在线网络转换中产生值Tue, 14 Jun 2011 06:04:16 GMT
)。我如何以编程方式在两者之间进行转换?
答案 0 :(得分:143)
OBJ - C: 使用dateWithTimeIntervalSince1970:,
NSDate *date = [NSDate dateWithTimeIntervalSince1970:timeStamp];
<强> SWIFT:强> 使用init(timeIntervalSince1970:)
let date = NSDate(timeIntervalSince1970:timeStamp)
答案 1 :(得分:18)
您可以使用方法dateWithTimeIntervalSince1970:
来转换您的时间戳,这是时间戳。
NSDate * myDate = [NSDate dateWithTimeIntervalSince1970:1308031456];
答案 2 :(得分:4)
double timeStamp = 1513330403393;
NSTimeInterval unixTimeStamp = timeStamp / 1000.0;
NSDate *exactDate = [NSDate dateWithTimeIntervalSince1970:unixTimeStamp];
NSDateFormatter* dateFormatter = [[NSDateFormatter alloc] init];
dateFormatter.dateFormat = @"dd/MM/yyy hh:mm a";
NSString *finalate = [dateFormatter stringFromDate:exactDate];
let timeStamp = 1513330403393
let unixTimeStamp: Double = Double(timeStamp) / 1000.0
let exactDate = NSDate.init(timeIntervalSince1970: unixTimeStamp)
let dateFormatt = DateFormatter();
dateFormatt.dateFormat = "dd/MM/yyy hh:mm a"
print(dateFormatt.string(from: exactDate as Date))
答案 3 :(得分:2)
请参阅有关日期格式的各种示例以及从苹果的时间戳获取日期
Use Formatter Styles to Present Dates and Times With the User’s Preferences
答案 4 :(得分:0)
这是一段代码,可让您将 unix 类型的时间戳转换为格式化日期
NSString * timeStampString =@"1304245000";
NSTimeInterval _interval=[timeStampString doubleValue];
NSDate *date = [NSDate dateWithTimeIntervalSince1970:_interval];
NSDateFormatter *_formatter=[[NSDateFormatter alloc]init];
[_formatter setDateFormat:@"dd.MM.yyyy"];
NSString *_date=[_formatter stringFromDate:date];
这将打印 _date 类似于 05.03.2011