I have searched Parse blog to see if I can get the solution to this, but didn't get any satisfactory answer. So I though I will clearly ask the question here with all the details so anyone else stuck at similar situation will find it easy to work.
Need: I have a departure and return text field which are updated using a single UIDatePicker. I want to save the selected dates in my Parse.com database. To which I can query and filter data. I also want to store local timezone in parse if possible. I am working with checking number of days but it was not relevant to this question so not including the code for that.
Success & Problem: I am able to save correct date and time in String format , but when I try to save in NSDate format using the code below, I get wrong time.
For ex: my date selection and stored result in parse are as below:
Departure date:
Date Picker selection: 01/May/2015 01:00 AM +0530
Stored date in Parse: Apr 30, 2015, 19:30
Return Date:
Date Picker selection: 02/May/2015 01:00 AM +0530
Stored date in Parse: May 01, 2015, 19:30
//My code is as below:
@IBOutlet var dOfTravelText: UITextField!
@IBOutlet var returnDateText: UITextField!
lazy var dateFormatter: NSDateFormatter = {
let dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "dd/MMM/yyyy hh:mm a Z"
// dateFormatter.dateStyle = .MediumStyle
// dateFormatter.timeStyle = .ShortStyle
return dateFormatter
}()
@IBAction func travelDatePicker(sender: UITextField) {
datePickerView.minimumDate = NSDate()
datePickerView.datePickerMode = UIDatePickerMode.DateAndTime
sender.inputView = datePickerView
timeSelected = sender
datePickerView.addTarget(self, action: "handleDatePicker:", forControlEvents: UIControlEvents.AllEvents)
}
// Date Picker target - Displaying date in textfield
func handleDatePicker(sender: UIDatePicker) {
//var dateFormatter = NSDateFormatter()
//dateFormatter.dateFormat = "mm/dd/yyyy"
timeSelected.text = dateFormatter.stringFromDate(sender.date)
println("From Date \(dOfTravelText.text!)")
println("To Date \(returnDateText.text!)")
}
// Submitting the dates to parse backend
@IBAction func postBtn(sender: AnyObject) {
let date = NSDate()
let calendar = NSCalendar.currentCalendar()
let components = calendar.components(.CalendarUnitHour | .CalendarUnitMinute, fromDate: date)
let dateMakerFormatter = NSDateFormatter()
dateMakerFormatter.dateFormat = "dd/MMM/yyyy hh:mm a Z"
let dD = dateMakerFormatter.dateFromString("\(dOfTravelText.text!)")!
let departureD = NSDateFormatter.localizedStringFromDate(dD, dateStyle: .MediumStyle, timeStyle: .ShortStyle)
println("From-------...\(departureD)")
let rD = dateMakerFormatter.dateFromString("\(returnDateText.text!)")!
let returnD = NSDateFormatter.localizedStringFromDate(rD, dateStyle: .MediumStyle, timeStyle: .ShortStyle)
println("To-------\(returnD)")
var userPost = PFObject(className:"UserPost")
userPost["departureDate"] = dD // Works but has Wrong time
userPost["rDate"] = rD // Works but Wrong time
userPost["travelDate"] = dOfTravelText.text // Works but it is stored in String format
userPost["returnDate"] = returnDateText.text // Works but it is stored in string format
userPost.saveInBackgroundWithBlock {
(success, error: NSError?) -> Void in
if (success) {
// The object has been saved.
println("Saved")
} else {
// There was a problem, check error.description
println("Error")
}
}
}
}
// Parse database column and type travelDate & returnDate are defined as of type "String"
departureDate & rDate are defined as Of type "Date"
Just for information: I am using Platform - iOS Swift (xcode 6.3, swift 1.2) Database backend = Parse.com
Since Parse stores the dates referenced to GMT, when you check them on parse or retrieve them, you may find this difference to your local timezone. In general dates are stored in GMT.
You can add an extra field in your Parse database and store the local timezone there. When retrieving data you can then use that information to interpret the date in the zone it is referenced in.
Depending on the type of data you store it might be okay to always interpret the date in the users local timezone, even when this has changed. You also could ask the user for a resolution if saved timezone and user timezone are different (so the user has moved).