0
NSDate *today = [NSDate dateWithTimeIntervalSince1970:(NSInteger)[FIRServerValue timestamp]/1000];

I am use above code to convert [FIRServerValue timestamp] into NSDate in my ios app. But I could't get the expected result. It returns 1970-03-13 11:37:47 +0000. So how to get current server time using [FIRServerValue timestamp].

isuru
  • 3,385
  • 4
  • 27
  • 62
  • 2
    IIRC one of them is in seconds, the other in milliseconds. So you need to divide by or multiple by 1000. Also see https://stackoverflow.com/questions/29243060/trying-to-convert-firebase-timestamp-to-nsdate-in-swift – Frank van Puffelen Aug 23 '17 at 12:37
  • 1
    `NSTimeInterval` is a floating-point value where the whole numbers represent whole seconds, so don't cast to `NSInteger`. – jscs Aug 23 '17 at 12:44
  • @FrankvanPuffelen I divided it by 1000 as mentioned. – isuru Aug 23 '17 at 13:03
  • 1
    Ah right... I forgot to scroll right. :-) `FIRServerValue timestamp]` is not convertible to a timestamp like that. It's not an actual timestamp, but a dictionary that tells the Database **Server** to write the current timestamp. Kat's answer to the question I linked shows how to write to the server and then get the actual timestamp back in the client. – Frank van Puffelen Aug 23 '17 at 13:12
  • @FrankvanPuffelen So can't we get the current timestamp from server? I need to get current timestamp from the server without writing it on server. – isuru Aug 23 '17 at 13:19
  • 1
    That is not possible. You can approximate it with the approach described here: https://stackoverflow.com/questions/43203910/how-to-create-negative-firebase-timestamp-in-swift/43394905#43394905 But it will be an approximation, it may be close - but it may also be off by a lot. – Frank van Puffelen Aug 23 '17 at 14:28

0 Answers0