Trying to convert Firebase timestamp to NSDate in Swift
ServerValue.timestamp()
works a little differently than setting normal data in Firebase. It does not actually provide a timestamp. Instead, it provides a value which tells the Firebase server to fill in that node with the time. By using this, your app's timestamps will all come from one source, Firebase, instead of whatever the user's device happens to say.
When you get the value back (from a observer), you'll get the time as milliseconds since the epoch. You'll need to convert it to seconds to create an NSDate. Here's a snippet of code:
let ref = Firebase(url: "")
// Tell the server to set the current timestamp at this location.
ref.setValue(ServerValue.timestamp())
// Read the value at the given location. It will now have the time.
ref.observeEventType(.Value, withBlock: {
snap in
if let t = snap.value as? NSTimeInterval {
// Cast the value to an NSTimeInterval
// and divide by 1000 to get seconds.
println(NSDate(timeIntervalSince1970: t/1000))
}
})
You may find that you get two events raised with very close timestamps. This is because the SDK will take a best "guess" at the timestamp before it hears back from Firebase. Once it hears the actual value from Firebase, it will raise the Value event again.
Convert Firebase Firestore Timestamp to Date (Swift)?
Either do:
let date = postTimestamp.dateValue()
or you could do:
let date = Date(timeIntervalSince1970: postTimestamp.seconds)
See the Timestamp reference documentation.
Swift 4: Firebase Timestamp
You can convert Firebase timestamp to NSDate
and then compare Date()
later on. Here is a similar question and a possible solution:
Trying to convert Firebase timestamp to NSDate in Swift
Getting the current Date from Firebase- Swift
The answer is in the link you provided. Get the timestamp from firebase and then call .dateValue() on the variable. The call would be something like:
let myDate: Date = timestamp.dateValue()
Converting Firestore Timestamp to Date data type
This is the message from the debugger
The behavior for system Date objects stored in Firestore is going to change AND YOUR APP MAY BREAK.
To hide this warning and ensure your app does not break, you need to add the following code to your app before calling any other Cloud Firestore methods:
let db = Firestore.firestore()
let settings = db.settings
settings.areTimestampsInSnapshotsEnabled = true
db.settings = settings
With this change, timestamps stored in Cloud Firestore will be read back as Firebase Timestamp objects instead of as system Date objects. So you will also need to update code expecting a Date to instead expect a Timestamp. For example:
// old:
let date: Date = documentSnapshot.get("created_at") as! Date
// new:
let timestamp: Timestamp = documentSnapshot.get("created_at") as! Timestamp
let date: Date = timestamp.dateValue()
Please audit all existing usages of Date when you enable the new behavior. In a future release, the behavior will be changed to the new behavior, so if you do not follow these steps, YOUR APP MAY BREAK.
How to convert firebase timestamp into date in iOS objective-c?
try this need to convert this stamp into seconds
NSDate *date = [NSDate dateWithTimeIntervalSince1970:(1493292606524/1000)];
NSLog(@"Date: %@", date);
Related Topics
Uibutton Action in Table View Cell
Trying to Handle "Back" Navigation Button Action in Ios
Sending Data With Segue With Swift
Anyobject Not Working in Xcode8 Beta6
How to Add an In-App Purchase to an iOS Application
Event Handling For iOS - How Hittest:Withevent: and Pointinside:Withevent: Are Related
Programmatically Align a Toolbar on Top of the Iphone Keyboard
Xcode 4: Create Ipa File Instead of .Xcarchive
Manual Language Selection in an Ios-App (Iphone and Ipad)
Looping a Video With Avfoundation Avplayer
Using Ssl in an Iphone App - Export Compliance
How to Detect the End of Loading of Uitableview
Create Uiimage With Solid Color in Swift
How to Tell Swiftui Views to Bind to Nested Observableobjects
Execute Action When Back Bar Button of Uinavigationcontroller Is Pressed