0

I have a separate DatePicker and TimePicker component in my app.

Once the user has selected both the desired Date and Time, I construct a new Date object like this:

let timeStamp = Date(year: selectedDate.year, month: selectedDate.month, day: selectedDate.day, hour: selectedTime.hour, minute: selectedTime.minute)

I then use DateFormatter to output the exact time that the user has selected like this:

let formatter = DateFormatter()
formatter.dateFormat = "HH:mm"
formatter.string(from: timeStamp)

Now I have a very weird bug where sometimes time output will be correct (time will be displayed in UTC+2) and sometimes it'll be incorrect (time will be displayed in UTC+1) and I have absolutely no idea what could be causing this.

Example 1 (correct output):

User selects: May 26, 2020 - 18:38
Date ISO output: "2020-05-26T16:38:00Z" 
DateFormatter output: "18:38"   

This is the correct output

Example 2 (wrong output):

User selects: March 26, 2020 - 18:38
Date ISO output: "2020-03-26T16:38:00Z"
DateFormatter output: "17:38"       

This is not the correct output. Time should be 18:38 like in the above example.

Someone please tell me how is this possible? Literally the only difference is user picked March instead of May (different month) and that for some reason confuses the DateFormatter, so Time output is in a different timezone.

I am using SwiftDate to work with dates in general.

Guy
  • 6,414
  • 19
  • 66
  • 136
  • Could it be that it accounts for daylight saving time? – New Dev May 26 '20 at 15:55
  • I haven't thought about that. Can you let me know how I would go about fixing that if this is the case? I'll be honest Dates are my weak point. I've put a lot of time into this and I just can't seem to figure it out. – Guy May 26 '20 at 15:58
  • This might help - https://codesections.wordpress.com/2018/09/21/daylight-may-ruin-your-date/ – New Dev May 26 '20 at 16:06

2 Answers2

0

That is probably because in May daylight saving is in effect and the difference to UTC changes from +1 to +2 hours

You can use the current TimeZone and add configure your DateFormatter with it

let timezone = TimeZone.current
dateFormatter.timeZone = timezone

That should make sure that you always use the same timezone that is currently used by your device

Robin Bork
  • 297
  • 2
  • 8
  • What would be the fix for this then? If the user selects 18:00 from the time picker, the value should still be displayed as 18:00 and not 17:00. It's confusing – Guy May 26 '20 at 15:59
0

Set correct formatter.locale, you can try Locale(identifier: "en_US_POSIX") or try to use formatter.timeZone property. Maybe TimeZone.current or TimeZone(secondsFromGMT: 0) will fix your problem

саня
  • 36
  • 2