Friday, November 6, 2026
HomeiOS Developmentios - Why am I getting timezone variations when changing date and...

ios – Why am I getting timezone variations when changing date and getting present date?

[ad_1]

Within the following code instance:

func numberOfDaysBetween(toDate: String) -> Int { // toDate = "2021/12/21"

    let dateFormatter = DateFormatter()
    dateFormatter.timeZone = TimeZone.present
    dateFormatter.dateFormat = "yyyy/MM/dd"
    
    let currentDate = Date()
    let toDateFormatted = dateFormatter.date(from: toDate)
    
    print ("Present Date:     (currentDate)")         // Present Date:     2021-12-21 11:50:12 +0000
    print ("ToDate:           (toDate)")              // ToDate:           2021/12/21
    print ("ToDateFormatted:  (toDateFormatted)")     // ToDateFormatted:  Non-obligatory(2021-12-20 13:30:00 +0000)
    print (dateFormatter.timeZone)                     // Non-obligatory(Australia/Adelaide (mounted (equal to present)))
    
    return 1 // Take a look at worth
}

I’m not seeing appropriate dates. I’ve spent 4 hours making an attempt numerous choices, however maintain coming again to the identical output. How do I see the anticipated output under?

I’m anticipating to see the next:

    print ("Present Date:     (currentDate)")         // Present Date:     2021-12-21
    print ("ToDate:           (toDate)")              // ToDate:           2021/12/21
    print ("ToDateFormatted:  (toDateFormatted)")     // ToDateFormatted:  2021/12/21
    print (dateFormatter.timeZone)                     // Non-obligatory(Australia/Adelaide (mounted (equal to present)))

Apparently, I’m situated in Adelaide, and the time is 22:20 (10:20 PM). Why is the time totally different when calling Date()?

[ad_2]

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments