I searched the Analysis blog to find out if I could get a solution to this question, but I didn't get a satisfactory answer. Therefore, although I will clearly ask the question here with all the details so that anyone else is stuck in a similar situation, it will work easily.
Need: I have a send and return text box that is updated using a single UIDatePicker. I want to save the selected dates in my Parse.com database. What can I request and filter data on. I also want to keep the local time zone in parsing, if possible. I work with checking the number of days, but this does not apply to this issue, therefore, not including a code for this.
Success and problem: I can save the correct date and time in String format, but when I try to save in NSDate format using the code below, I get the wrong time.
For example: my date picker and saved result in parsing are as follows:
Departure Date: Selection Date Selection: 01 / May / 2015 01:00 +0530
Saved date at Parse: April 30, 2015 7:30 p.m.
Return Date: Selection Date Selection: 02 / May / 2015 01:00 +0530
Saved date at Parse: May 01, 2015 7:30 p.m.
// My code is as follows:
@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") } } } }
// Database parsing and column type travelDate and returnDate are defined as type "String"
departureDate and rDate are defined as a Date Type
For information only: I am using Platform - iOS Swift (xcode 6.3, swift 1.2) Database backend = Parse.com