I currently store dates in Parse, based on the US/Eastern time zone. However, when I get the objects from Parse and set their date properties, the time is always displayed as one hour earlier than the expected time.
I believe this is a result of daylight savings time, but I am unsure how to correct the issue without hardcoding the addition of an extra hour.
var objectDate = object.createdAt! // object is from Parse query
let calendar = NSCalendar.currentCalendar() // I am in Pacific timezone
var dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "E, M/d, h:mm a"
dateFormatter.timeZone = NSTimeZone(name: "US/Eastern") // date is 8 am Eastern time
var formattedDate = dateFormatter.stringFromDate(objectDate)
// formattedDate prints as 4 am, but should be 5 am
It helps to think about dates as not having a timezone as part of them. What I mean is, when you say "Save the time and day right now" the NSDate object should use UTC time, because it really doesn't matter. When you go to look at the date, you view it from a certain timezone or format perspective:
So, it could be like this:
//this saves the date as NOW, whenever it is called
var date = NSDate()
//it doesn't matter what timezone you want it to save in, because it always
//saves in a way that it KNOWS when it occurred
//when I want to view the value of the date from a "PST" timezone perspective
//I need to use an NSDateFormatter to set my point of view for the date
let formatter = NSDateFormatter()
formatter.timeZone = NSTimeZone(name: "PST") //sets formatter to show what PST zone is
formatter.dateFormat = "hh:mm a z" //formats the time to show as 07:30 PST
//now that we have the date formatted, we could do something like set a label to it
@IBOutlet var dateLabel: UILabel!
dateLabel.text = formatter.stringFromDate(date)
So, as you see above, the date isn't really saved as any particular timezone. It is simply saved in a way that the code can figure out the exact time/date. You then view the date however you want when you refer to it and use an NSDateFormatter.