Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Timezone issue with UIDatePicker Date: 1 Hour Wrong

I'm having an issue with the Monotouch UIDatePicker being 1 hour behind. I think this is to do with Time Zones or something similar. I've tried explicitly setting the TimeZone and Locale of my UIDatePicker but this doesn't seem to be helping.

datePicker.Locale = NSLocale.CurrentLocale;
datePicker.TimeZone = NSTimeZone.LocalTimeZone;

In the ValueChanged handler the following line returns a value 1 hour earlier than the time selected in the user interface:

var date = DateTime.SpecifyKind((s as UIDatePicker).Date, DateTimeKind.Local).ToLocalTime();

In the ValueChanged Handler I've double checked that the Locale and TimeZone is the same as what was set. The TimeZone is Europe/Dublin and Locale en_US. This information was retrieved by:

datePicker.Locale.LocaleIdentifier;
datePicker.DatePicker.TimeZone;

Is there another step I'm missing?

Thanks!

like image 336
binncheol Avatar asked Sep 06 '13 11:09

binncheol


1 Answers

The date returned from DatePicker is in UTC format. There are several methods of converting UTC to local time. As this answer states ToLocalTime is the best one.

DateTime.SpecifyKind(datePicker.Date, DateTimeKind.Utc).ToLocalTime();
like image 110
Daria Trainor Avatar answered Oct 17 '22 21:10

Daria Trainor