WWDC22 SALE: Save 50% on all my Swift books and bundles! >>

Time on date shows 6 hours ahead

Forums > Swift

I am trying to create a date and time that I can set a notification to. e.g., Today at 10AM. I have a picker where you can select howManyDays from current alertTime(which is a date from a DatePicker that only shows hour and minute). The issue is that when I compute the date the date is fine, but the time on the date is set to 6 hours ahead. I am stumpped as to why? I thought that when selecting a date it always uses the current timezone. How can I get a date with the correct time?

What is interesting is that I have put a print statement to see what hour component is coming back and it is set correctly however, newDate's time will display 6 hours ahead.

Here is the code I wrote:

let cal = Calendar.current

let timeComponents = cal.dateComponents([.hour, .minute], from: alertTime)
let newDateInPast = cal.date(byAdding: .day, value: -howManyDays, to: Date())
let dateComponents = cal.dateComponents([.month, .day, .year], from: newDateInPast!)

var mergedComponents = DateComponents()
mergedComponents.year = dateComponents.year
mergedComponents.month = dateComponents.month
mergedComponents.day = dateComponents.day
mergedComponents.hour = timeComponents.hour
mergedComponents.minute = timeComponents.minute

let newDate = cal.date(from: mergedComponents)
print("\(timeComponents.hour)")
print("How Many Days: \(newDate)")

Thanks in advance for your help, Taz

   

Taz questions his sanity:

The issue is that when I compute the date the date is fine, but the time on the date is set to 6 hours ahead. I am stumpped as to why?

@twoStraws notes that Dates are hard! See-> Working with Dates

You need to understand the differences between Date and Calendar.

Apple documentation says this about Date:

The Date structure provides methods for comparing dates, calculating the time interval between two dates, and creating a new date from a time interval relative to another date. Use date values with DateFormatter instances to create localized representations of dates and times and with Calendar instances to perform calendar arithmetic.

Here's some code for you to paste into Playgrounds. (You ARE using Playgrounds, yes?!)

// Fun with Dates and Calendars!
import Foundation

let myCalendar = Calendar.current
myCalendar.dateComponents([.timeZone], from: Date()) // timeZone: America/New_York

let rightNow    = Date()
let howManyDays = 4

// Check values
rightNow.description // notice the time is a UTC value. Dates don't know timeZones.

let newDateInPast  = myCalendar.date(byAdding: .day, value: -howManyDays, to:  rightNow)
let dateComponents = myCalendar.dateComponents([.month, .day, .year, .hour, .minute, .timeZone], from: newDateInPast!)

var mergedComponents    = DateComponents(calendar: myCalendar,
                                         timeZone: dateComponents.timeZone,
                                         year:     dateComponents.year,
                                         month:    dateComponents.month,
                                         day:      dateComponents.day,
                                         hour:     dateComponents.hour,
                                         minute:   dateComponents.minute)
// Double check your assumptions!
mergedComponents.year
mergedComponents.month
mergedComponents.day
mergedComponents.hour
mergedComponents.minute
mergedComponents.timeZone  // Should be the same as your default calendar.

let newDate = myCalendar.date(from: mergedComponents) // creating from the same calendar
newDate?.description // notice the time is a UTC value. Dates don't know timeZones.

From this you can see that your Calendar is synchronized with you Mac's local time zone. Dates, on the otherhand, are just points in time stored and calculated as UTC values. As the Apple documentation states:

Use date values with DateFormatter instances to create localized representations of dates and times

   

Thanks @Obelix,

Yes I was and frankly am still questioning my sanity. LOL.

I had read that article from Paul called **Working With Date**. I guess I didn't understand the whole UTC thing and that dates don't know timezones.

Thanks for the code example, it made it a little more clear to me. So, if I undestand what I saw in the print statements, the date is set correctly, but when you print it out its in the UTC format and therefore doesn't know the TimeZone.

Thanks, Taz

   

Hacking with Swift is sponsored by Emerge

SPONSORED Optimize your app’s startup time, binary size, and overall performance using Emerge’s advanced app optimization and monitoring tools. Reliably measure app size, speed up your app's startup time with Emerge's Launch Booster, and much more. Emerge is actively used by many of the top mobile development teams in the world.

Find out more

Sponsor Hacking with Swift and reach the world's largest Swift community!

Reply to this topic…

You need to create an account or log in to reply.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.