Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set timezone on a UIDatePicker?

Is there any way to set a UIDatePicker's timezone to mine (e.g. GMT+3)?

I have tried a lot of solutions on the internet, yet none work.

like image 462
Myz Avatar asked Dec 02 '25 23:12

Myz


1 Answers

This is from the Apple docs:

Configuring a Date Picker The configuration of a date picker is determined by the datePickerMode property, whose value you can set programmatically or in Interface Builder. For modes that include date or time values, you can also configure the locale, calendar, and time zone information as appropriate. The date picker uses that information when formatting date and time values for the current user, and defaults to the device’s locale, calendar and time zone. The date property represents the currently selected date in the form of an NSDate object, which is calendar and time zone agnostic.

You can set the time zone by configuring the date picker. TimeZone.current will initialise a TimeZone object with the devices current time zone. You can also configure the calendar or locale if needed (though they default to the current device setting).

var picker = UIDatePicker()
picker.calendar = Calendar.current
picker.locale = Locale.current
picker.timeZone = TimeZone.current

You can retrieve the current date with the picker.date property. This can also be set programmatically to animate the date picker to that value.


You can use the date from the datepicker separately as well. You need a date formatter to apply calendar and timezone information:

let date = picker.date
let formatter = DateFormatter()
// Optionally set calendar, timezone etc on formatter
print(formatter.string(from: date))
like image 148
Chris Avatar answered Dec 04 '25 12:12

Chris



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!