在Swift中的日期到毫秒和更新

我花了当前的时间,用UTC,并把它放在nanaoseconds然后我需要花费纳秒并回到当地时间的日期。 我能够得到时间到纳秒然后回到日期字符串,但是当我从字符串到日期时,时间会变得复杂。

//Date to milliseconds func currentTimeInMiliseconds() -> Int! { let currentDate = NSDate() let dateFormatter = DateFormatter() dateFormatter.dateFormat = format dateFormatter.timeZone = NSTimeZone(name: "UTC") as TimeZone! let date = dateFormatter.date(from: dateFormatter.string(from: currentDate as Date)) let nowDouble = date!.timeIntervalSince1970 return Int(nowDouble*1000) } //Milliseconds to date extension Int { func dateFromMilliseconds(format:String) -> Date { let date : NSDate! = NSDate(timeIntervalSince1970:Double(self) / 1000.0) let dateFormatter = DateFormatter() dateFormatter.dateFormat = format dateFormatter.timeZone = TimeZone.current let timeStamp = dateFormatter.string(from: date as Date) let formatter = DateFormatter() formatter.dateFormat = format return ( formatter.date( from: timeStamp ) )! } } 

//时间戳是正确的,但返回的日期不是

我不明白为什么你要用字符串做任何事情……

 extension Date { var millisecondsSince1970:Int { return Int((self.timeIntervalSince1970 * 1000.0).rounded()) } init(milliseconds:Int) { self = Date(timeIntervalSince1970: TimeInterval(milliseconds) / 1000) } } Date().millisecondsSince1970 // 1476889390939 Date(milliseconds: 0) // "Dec 31, 1969, 4:00 PM" (PDT variant of 1970 UTC) 

正如@Travis解决方案有效,但在某些情况下

var millisecondsSince1970:Int 将导致崩溃应用程序

有错误

Double值无法转换为Int,因为如果发生,结果将大于Int.max 请使用Int64更新您的答案

这是更新的答案

 extension Date { var millisecondsSince1970:Int64 { return Int64((self.timeIntervalSince1970 * 1000.0).rounded()) //RESOLVED CRASH HERE } init(milliseconds:Int) { self = Date(timeIntervalSince1970: TimeInterval(milliseconds / 1000)) } } 

希望对有同样问题的人也有帮助

 //Date to milliseconds func currentTimeInMiliseconds() -> Int { let currentDate = Date() let since1970 = currentDate.timeIntervalSince1970 return Int(since1970 * 1000) } //Milliseconds to date extension Int { func dateFromMilliseconds() -> Date { return Date(timeIntervalSince1970: TimeInterval(self)/1000) } } 

我通过字符串删除看似无用的转换和所有随机的!

@Travis解决方案是正确的,但是在生成Date时它会丢失毫秒。 我添加了一行以包含日期中的毫秒数:

如果您不需要此精度,请使用Travis解决方案,因为它会更快。

 extension Date { func toMillis() -> Int64! { return Int64(self.timeIntervalSince1970 * 1000) } init(millis: Int64) { self = Date(timeIntervalSince1970: TimeInterval(millis / 1000)) self.addTimeInterval(TimeInterval(Double(millis % 1000) / 1000 )) } } 
 let dateTimeStamp = NSDate(timeIntervalSince1970:Double(currentTimeInMiliseconds())/1000) //UTC time //YOUR currentTimeInMiliseconds METHOD let dateFormatter = NSDateFormatter() dateFormatter.timeZone = NSTimeZone.localTimeZone() dateFormatter.dateFormat = "yyyy-MM-dd" dateFormatter.dateStyle = NSDateFormatterStyle.FullStyle dateFormatter.timeStyle = NSDateFormatterStyle.ShortStyle let strDateSelect = dateFormatter.stringFromDate(dateTimeStamp) print("Local Time", strDateSelect) //Local time let dateFormatter2 = NSDateFormatter() dateFormatter2.timeZone = NSTimeZone(name: "UTC") as NSTimeZone! dateFormatter2.dateFormat = "yyyy-MM-dd" let date3 = dateFormatter.dateFromString(strDateSelect) print("DATE",date3)