Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Date to milliseconds and back to date in Swift

Tags:

date

time

ios

swift

I am taking the current time, in UTC, and putting it in nanaoseconds and then I need to take the nanoseconds and go back to a date in local time. I am able to do get the time to nanoseconds and then back to a date string but the time gets convoluted when I go from a string to date.

    //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 ) )!         }     } 

//The timestamp is correct but the date returned isn't

like image 408
user1079052 Avatar asked Oct 19 '16 14:10

user1079052


People also ask

How do you convert date to Millisec?

Once you have the Date object, you can get the milliseconds since the epoch by calling Date. getTime() . The full example: String myDate = "2014/10/29 18:10:45"; //creates a formatter that parses the date in the given format SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss"); Date date = sdf.

What is timeIntervalSince1970?

timeIntervalSince1970 is the number of seconds since January, 1st, 1970, 12:00 am (mid night) timeIntervalSinceNow is the number of seconds since now.

What is the date format in Swift?

dateFormat = "yyyy-MM-dd'T'HH:mm:ss.


2 Answers

I don't understand why you're doing anything with strings...

extension Date {     var millisecondsSince1970:Int64 {         Int64((self.timeIntervalSince1970 * 1000.0).rounded())     }          init(milliseconds:Int64) {         self = Date(timeIntervalSince1970: TimeInterval(milliseconds) / 1000)     } }   Date().millisecondsSince1970 // 1476889390939 Date(milliseconds: 0) // "Dec 31, 1969, 4:00 PM" (PDT variant of 1970 UTC) 
like image 57
Travis Griggs Avatar answered Sep 22 '22 13:09

Travis Griggs


As @Travis Solution works but in some cases

var millisecondsSince1970:Int WILL CAUSE CRASH APPLICATION ,

with error

Double value cannot be converted to Int because the result would be greater than Int.max if it occurs Please update your answer with Int64

Here is Updated Answer

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

About Int definitions.

On 32-bit platforms, Int is the same size as Int32, and on 64-bit platforms, Int is the same size as Int64.

Generally, I encounter this problem in iPhone 5, which runs in 32-bit env. New devices run 64-bit env now. Their Int will be Int64.

Hope it is helpful to someone who also has same problem

like image 37
Prashant Tukadiya Avatar answered Sep 18 '22 13:09

Prashant Tukadiya