Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using DateFormatter on a Unix timestamp

I get a crash when running and it points at the dateFormmater.timezone. The error in the console is:

Could not cast value of type 'Swift.Optional' (0x1192bf4a8) to 'NSTimeZone' (0x1192c0270).

the value of rowEvents.date is "1480134638.0"

Im trying to pull out a Unix timestamp from Firebase saved as a string. Convert it to Date and again save it as a string so I can post it on a cell label.

I got this code from StackOverflow. I plugged in my data and everything is all good until I run it. I guess everything is not all good...

if let lastUpdated : String = rowEvents.date {      let epocTime = TimeInterval(lastUpdated)! / 1000 // convert it from milliseconds dividing it by 1000      let unixTimestamp = NSDate(timeIntervalSince1970: epocTime) //convert unix timestamp to Date      let dateFormatter = DateFormatter()     dateFormatter.timeZone = NSTimeZone() as TimeZone!     dateFormatter.locale = NSLocale.current // NSLocale(localeIdentifier: "en_US_POSIX")     dateFormatter.dateFormat =  "yyyy-MM-dd'T'HH:mm:ssZZZZZ"     dateFormatter.date(from: String(describing: unixTimestamp))      let updatedTimeStamp = unixTimestamp     let cellDate = DateFormatter.localizedString(from: updatedTimeStamp as Date, dateStyle: DateFormatter.Style.full, timeStyle: DateFormatter.Style.medium)      cell.subtitleLabel.text = cellDate               } 

The result came from this code here:

let myTimeStamp = self.datePicker?.date.timeIntervalSince1970  let calendarDate = String(describing: myTimeStamp! /** 1000*/) 
like image 690
CRey Avatar asked Nov 17 '16 06:11

CRey


People also ask

How Unix timestamp is calculated?

Unix time is a way of representing a timestamp by representing the time as the number of seconds since January 1st, 1970 at 00:00:00 UTC.

Is Unix timestamp a UTC?

Unix timestamps are always based on UTC (otherwise known as GMT). It is illogical to think of a Unix timestamp as being in any particular time zone. Unix timestamps do not account for leap seconds.

How do I convert Unix timestamp to local time in Excel?

Since a day contains 86400 seconds (24 hours x 60 minutes x 60 seconds), conversion to Excel time can be done by dividing days by 86400 and adding the date value for January 1st, 1970. When C5 is formatted with the Excel date "d-mmm-yyyy", the date is displayed as 1-Oct-2018.


2 Answers

You can convert unixTimestamp to date using Date(timeIntervalSince1970:).

let unixTimestamp = 1480134638.0 let date = Date(timeIntervalSince1970: unixTimestamp) 

If you want to display date in string with specific formate than you can use DateFormatter like this way.

let date = Date(timeIntervalSince1970: unixtimeInterval) let dateFormatter = DateFormatter() dateFormatter.timeZone = TimeZone(abbreviation: "GMT") //Set timezone that you want dateFormatter.locale = NSLocale.current dateFormatter.dateFormat = "yyyy-MM-dd HH:mm" //Specify your format that you want let strDate = dateFormatter.string(from: date) 
like image 73
Nirav D Avatar answered Oct 05 '22 17:10

Nirav D


The problem is the line dateFormatter.timeZone = NSTimeZone() as TimeZone!.

Simply use TimeZone instead of NSTimeZone like
dateFormatter.timeZone = TimeZone.current and your code will work.

You might also remove your / 1000 because 1480134638.0 looks more like seconds than milliseconds (since 1970).

like image 23
d.felber Avatar answered Oct 05 '22 17:10

d.felber