Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS Swift - Get the Current Local Time and Date Timestamp

I'm trying to make an attendance app and I am really confused about date and time in iOS and Firebase.

I use date as Key, this is the structure of my Firebase database.

--Employees   --Unique_ID      --Details           Name: John      --Attendance           --dateToday               Timein: 8:00 AM               Timeout: 5:00 PM               BreakStart: 12:00 PM               BreakFinish: 1:00 PM 

This is my code to get the date timestamp I used as Key

 override func viewDidLoad() {      super.viewDidLoad()       let now = NSDate()      let nowTimeStamp = self.getCurrentTimeStampWOMiliseconds(dateToConvert: now)       // I save this dateToday as Key in Firebase      dateToday = nowTimeStamp }   func getCurrentTimeStampWOMiliseconds(dateToConvert: NSDate) -> String {     let objDateformat: DateFormatter = DateFormatter()     objDateformat.dateFormat = "yyyy-MM-dd"     let strTime: String = objDateformat.string(from: dateToConvert as Date)     let objUTCDate: NSDate = objDateformat.date(from: strTime)! as NSDate     let milliseconds: Int64 = Int64(objUTCDate.timeIntervalSince1970)     let strTimeStamp: String = "\(milliseconds)"     return strTimeStamp } 

But when I convert it back to date I get 2017-09-22 16:00:00 +0000, which is wrong because it is 23rd of September in my location.

What is the right code to use so that I can get the correct date timestamp and time timestamp?

like image 225
TSM Avatar asked Sep 23 '17 06:09

TSM


People also ask

How do I get todays date in Swift 5?

Get current time in “YYYY-MM–DD HH:MM:SS +TIMEZONE” format in Swift. This is the easiest way to show the current date-time.

What is timestamp in Swift?

Save. What is timestamp? A timestamp is that which contain some characters that are in an encoded form which will contain any event, date or time etc.

How to get current date and time in Swift?

To get current date and time in Swift, import Foundation, and initialize a Date value. Date value has a description property, through which we can get the textual description of the date value. The syntax to get the current date and time using Date is Of course, the date and time we get are the UTC +0 or Greenwich Mean Time.

What is the best way to store timestamp in Swift?

First I would recommend you to store your timestamp as a NSNumber in your Firebase Database, instead of storing it as a String. Another thing worth mentioning here, is that if you want to manipulate dates with Swift, you'd better use Date instead of NSDate, except if you're interacting with some Obj-C code in your app.

How to parse and format a date in Swift?

Use DateFormatter to parse and format a date in Swift. The default time zone for a DateFormatter is the device’s local time, so to obtain UTC time the timeZone value needs to be configured.

How many time zones are there in Swift?

The date format in Swift is yyyy-MM-dd'T'HH:mm:ssXXXXX but RFC3339 allows us to take advantage of the ISO8601DateFormatter: There are 37 or so other time zones we'd have to account for and it's up to you to determine which ones, because there is no definitive list. Some standards count fewer time zones, some more.


1 Answers

For saving Current time to firebase database I use Unic Epoch Conversation:

let timestamp = NSDate().timeIntervalSince1970 

and For Decoding Unix Epoch time to Date().

let myTimeInterval = TimeInterval(timestamp) let time = NSDate(timeIntervalSince1970: TimeInterval(myTimeInterval)) 
like image 68
Satish Babariya Avatar answered Sep 21 '22 16:09

Satish Babariya