0

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?

brimstone
  • 3,370
  • 3
  • 28
  • 49
Mych
  • 2,527
  • 4
  • 36
  • 65
  • try adding "formatter.locale = NSLocale.systemLocale()" – Surya Subenthiran Apr 17 '16 at 15:14
  • After doing some more searching I came across this which would probably solve my problem.... http://stackoverflow.com/questions/26189656/how-can-i-set-an-nsdate-object-to-midnight... The problem I have now is that the published answer give me an Instance member 'cal' cannot be used on type ViewController – Mych Apr 17 '16 at 16:47
  • All sorted... I've used the solution I found above... I just had to place the code within a function rather than directly into a class. – Mych Apr 18 '16 at 11:32

1 Answers1

0

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

Community
  • 1
  • 1
Mych
  • 2,527
  • 4
  • 36
  • 65