Good day! I'm trying to make a Date Picker using this date picker dialog. Now I'm having problem on setting the defaultDate
Option, It requires NSDate so i made this.
let date = NSDate()
let formatter1 = NSDateFormatter()
formatter1.dateFormat = "MMM dd"
formatter1.timeZone = NSTimeZone(abbreviation: "GMT")
formatter1.timeZone = NSTimeZone(forSecondsFromGMT: 28800)
let gmt8TimeZone = formatter1.stringFromDate(date)
let dateToday = formatter1.dateFromString(gmt8TimeZone)
print("\(gmt8TimeZone) - \(dateToday!)")
DatePickerDialog().show("Choose Date", doneButtonTitle: "Done", cancelButtonTitle: "Cancel", defaultDate: dateToday!, datePickerMode: UIDatePickerMode.Date) { (date) -> Void in
self.dateSelected = date
self.showTimePicker()
}
The problem is, It's not starting on the defaultDate
that i set which is dateToday
. So what I did is I print the date string which is gmt8TimeZone
and dateToday
and it gives me this Nov 25 - 2000-11-24 16:00:00 +0000
why is it giving me different dates? it should be the same right? What I have done wrong?
Simply provide NSDate() to the defaultDate parameter.
NSDate is basically a timestamp, a fixed and absolute point in time, and is not linked to any timeZone: NSDate() is absolutely, totally, now.
When you fiddle with NSDateFormatter to "fix" it, you move out of the absolute timestamp world to enter the strings world "yyyy-mm-dd..." which indeed require time zones to be interpreted as points in time. And since you mess with it, your date drifts.