格式化时间戳后返回错误的值 [英] Wrong value returned after formatting timestamp

查看:204
本文介绍了格式化时间戳后返回错误的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  let timestampDouble:Double = 1455970380471 $ b $我想尝试格式化时间戳,但返回错误的值。 b let timestamp = NSDate(timeIntervalSince1970:timestampDouble)
let formattedTimestamp = NSDateFormatter.localizedStringFromDate(timestamp,dateStyle:.MediumStyle,timeStyle:.ShortStyle)

formattedTimestamp返回 Jun 22,48115,8:49 AM 而不是正确的时间戳 Feb 20 ,2016,11:13 PM (来自



一旦链接到 Epoch Converter ,我把你原来的价值粘贴在这里,确实获得了二月的正确价值。但是请注意粗体文本。


假设此时间戳记以毫秒为单位:


该网站看到这个数字将会给您一个未来的46,000年的日期和您实际给予的猜测毫秒,所以它基于这个假设进行计算。



NSDate timeIntervalSince1970 构造函数接受类型为 NSTimeInterval 的参数,它始终是的度量,而不是毫秒。您需要将原始值除以1000以获取秒数,或者写入一个 NSDate 构造函数,该函数希望以1970年以来的毫秒数初始化,而不是数字的秒数,作为您使用的初始化程序。


I am trying to format a timestamp but the wrong value is returned.

let timestampDouble: Double = 1455970380471
let timestamp = NSDate(timeIntervalSince1970: timestampDouble)
let formattedTimestamp = NSDateFormatter.localizedStringFromDate(timestamp, dateStyle: .MediumStyle, timeStyle: .ShortStyle)

formattedTimestamp returns Jun 22, 48115, 8:49 AM instead of the correct timestamp of Feb 20, 2016, 11:13 PM (from epochconverter.com).

解决方案

You've got the wrong value.

I reversed the process, starting with the date string you want, and ran it through this code:

let dateFormatter = NSDateFormatter()
dateFormatter.dateStyle = .MediumStyle
dateFormatter.timeStyle = .ShortStyle
let date = dateFormatter.dateFromString("Feb 20, 2016, 11:13 PM")
let timeInterval = date?.timeIntervalSince1970

In a Swift playground, timeInterval has a value of 1456031580 which is three digits shorter than the value you're using.

When I go back to your original code and use this new value:

let timestampDouble: Double = 1456031580
let timestamp = NSDate(timeIntervalSince1970: timestampDouble)
let formattedTimestamp = NSDateFormatter.localizedStringFromDate(timestamp, dateStyle: .MediumStyle, timeStyle: .ShortStyle)

We get the expected value: "Feb 20, 2016, 11:13 PM".

Of course, note that the exact time interval you get (the 1456031580 number) from the first snippet and the exact string you get out for that particular number will depend on your time zone.


Once you linked to Epoch Converter, I pasted your original value in here and indeed got the correct value of February something-ish. But it's important to note the bolded text.

Assuming that this timestamp is in milliseconds:

The website sees the number would give you a date 46,000 years in the future and guesses that you've actually given it milliseconds, so it makes a calculation based on that assumption.

NSDate's timeIntervalSince1970 constructor takes an argument of type NSTimeInterval, which is always a measure of seconds, not milliseconds. You need to divide your original value by 1000 to get the number of seconds, or write an NSDate constructor which expects to be initialized with the number of milliseconds since 1970 rather than the number of seconds, as the initializer you used expects.

这篇关于格式化时间戳后返回错误的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆