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.
You can of course use both, but the Documentation states:
Date bridges to the NSDate class. You can use these interchangeably in code that interacts with Objective-C APIs.
Now to answer your question, I think the problem here is because of the timezone.
For example if you print(Date())
, as for now, you would get:
2017-09-23 06:59:34 +0000
This is the Greenwich Mean Time (GMT).
So depending on where you are located (or where your users are located) you need to adjust the timezone before (or after, when you try to access the data for example) storing your Date
:
let now = Date()
let formatter = DateFormatter()
formatter.timeZone = TimeZone.current
formatter.dateFormat = "yyyy-MM-dd HH:mm"
let dateString = formatter.string(from: now)
Then you have your properly formatted String
, reflecting the current time at your location, and you're free to do whatever you want with it :) (convert it to a Date
/ NSNumber
, or store it directly as a String
in the database..)