I am storing a record and this record has a date pumpTimeNSDate
attached to it.
If the device that has created this record is placed on a different timezone from the device that is loading the record the device that is loading the record shows a different date from the date that is shown by the device that has created the record. I want them both to show the same date.
I understand that happens because the NSDateFormatter()
adjusts the NSDate (which is just a reference in time) to the users current timezone taking in consideration it's distance from GMT.
How do I get a device that is loading the record in a different timezone to show the same that is getting show on the device that has created the record?
Note: originTimeOffsetObject stores how far from GMT was the device that has created the record.
if let pumpTimeNSDate = objects[indexPath.row].objectForKey("pumpTimeNSDate") as? NSDate {
let formatter = NSDateFormatter()
formatter.dateFormat = "E dd, H:mm"
if let originTimeOffsetObject = objects[indexPath.row].objectForKey("originTimeOffset") as? NSTimeInterval
{
// not sure how to use originTimeOffsetObject
}
else
{
// if timeoffset do not exist on DB
createdAt = formatter.stringFromDate(pumpTimeNSDate)
}
}
You create and set an NSTimeZone onto a NSDateFormatter. Since you know the offset, you can:
let dateFormatter = NSDateFormatter()
dateFormatter.dateStyle = .MediumStyle
dateFormatter.timeStyle = .MediumStyle
dateFormatter.timeZone = NSTimeZone(forSecondsFromGMT: 0) // Update to your value
dateFormatter.stringFromDate(NSDate()) // Display your date