Mych
Mych

Reputation: 2563

Setting date for datepicker taking into account daylight saving Swift

I have a date picker with the mode set to Date (I'm only interested in Day, Month, Year). The date is set during viewDidLoad by

    let currentDate = NSDate()
    dp_MainDatePicker.date = currentDate
    dp_MainDatePicker.maximumDate = currentDate

This seems OK... but...

I store the date in a Singleton class which has var dateForLog: NSDate and in another viewcontroller I will save this date to core data...

newMileageLog.setValue(Data.sharedData.dateForLog, forKey: "tripDate")

then eventually I display the date in a TableViewCell...

//format date as medium style date
let formatter = NSDateFormatter()
formatter.dateStyle = .MediumStyle
let logDateString = formatter.stringFromDate(mileageLog.tripDate!)

cell.lb_LogDate.text = logDateString

Unfortunately this does not take into account daylight savings... e.g.

if the datepicker is set to Jan 14 2016 the date in the tableviewcell is Jan 14,2016

if datepicker us set to Apr 14 2016 the date in the tableviewcell is Apr 13, 2016. If I place a breakpoint I can see that even though the dials are set to Apr 14 2016 the dateForLog is showing 2016-04-13 23:00:00 +0000

How do I stop this from happening?

Upvotes: 0

Views: 355

Answers (1)

Mych
Mych

Reputation: 2563

After doing some more searching I came across this which solve my problem.... How can I set an NSDate object to midnight?...

Upvotes: 0

Related Questions