iOS Swift - Get the Current Local Time and Date Timestamp
Solution 1
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))
Solution 2
If you just want the unix timestamp, create an extension:
extension Date {
func currentTimeMillis() -> Int64 {
return Int64(self.timeIntervalSince1970 * 1000)
}
}
Then you can use it just like in other programming languages:
let timestamp = Date().currentTimeMillis()
Solution 3
in Swift 5
extension Date {
static var currentTimeStamp: Int64{
return Int64(Date().timeIntervalSince1970 * 1000)
}
}
call like this:
let timeStamp = Date.currentTimeStamp
print(timeStamp)
Thanks @lenooh
Solution 4
The simple way to create Current TimeStamp. like below,
func generateCurrentTimeStamp () -> String {
let formatter = DateFormatter()
formatter.dateFormat = "yyyy_MM_dd_hh_mm_ss"
return (formatter.string(from: Date()) as NSString) as String
}
Solution 5
If you code for iOS 13.0 or later and want a timestamp, then you can use:
let currentDate = NSDate.now
TSM
Updated on July 22, 2022Comments
-
TSM almost 2 years
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?
-
barrylachapelle over 5 yearsBy far the best answer.
-
Spire over 4 yearswill NSDate().timeIntervalSince1970 be calculating the timezone, or is there any way to do this for different timezones?
-
Andrew Koster over 4 yearsEpoch time, by definition, is in UTC. It's the number of seconds/milliseconds elapsed since midnight on January 1, 1970, in the UTC timezone. Time zone information would only be useful for displaying it in a specific human language, not for storing or calculating anything.
-
djdance over 2 yearsthis is not 10-digit timestamp