8

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!

Daria Trainor
  • 5,545
  • 4
  • 23
  • 30
binncheol
  • 1,393
  • 4
  • 22
  • 38
  • Is it possible that daylight savings is in effect? This can sometimes cause hour differences similar to what you seem to be experiencing. – Melvin DVaz Sep 10 '13 at 10:23

3 Answers3

12

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();
Community
  • 1
  • 1
Daria Trainor
  • 5,545
  • 4
  • 23
  • 30
  • 1
    I think this is the correct answer. In the original code, the OP specified `DateTimeKind.Local` instead of `DateTimeKind.Utc`. Since an `NSDate` represents UTC but the conversion doesn't set the `.Kind`, this is how you have to do it. – Matt Johnson-Pint Sep 16 '13 at 07:14
  • But datePicker.Date is an NSDate and the first parameter of the method DateTime.SpecifyKind is a DateTime... – leoneboaventura May 22 '15 at 10:52
2

After var date = DateTime ..., add the following:

if (date.IsDaylightSavingTime ())
    date = date.AddHours (1);
frogge
  • 274
  • 1
  • 3
  • 13
  • Daylight savings time is not always adjusted by an hour. http://www.timeanddate.com/time/dst/ http://infiniteundo.com/post/25509354022/more-falsehoods-programmers-believe-about-time-wisdom – Daria Trainor Sep 12 '13 at 02:37
  • Indeed. It's a quick fix. I'd rather be almost correct than completely wrong, so to speak. You could of course use a DST offset based on timezone. Do you have a nice proposal for this? I like the second link, but it doesn't give any answers; only states that we carry misconceptions. – frogge Sep 12 '13 at 07:58
  • The answer is in my answer below. Use DateFormatter, it takes care of DST for you. – Daria Trainor Sep 13 '13 at 02:39
  • Yes, but only when outputting as text – frogge Sep 13 '13 at 07:20
  • TimeZoneInfo.ConvertTimeFromUtc returns DateTime. – Daria Trainor Sep 14 '13 at 09:04
2

This magical code fixed all my problems with daylight savings and timezones. Write if you need help.

NSDate sourceDate = date;

NSTimeZone sourceTimeZone = new NSTimeZone ("UTC");
NSTimeZone destinationTimeZone = NSTimeZone.LocalTimeZone;

int sourceGMTOffset = sourceTimeZone.SecondsFromGMT (sourceDate);
int destinationGMTOffset = destinationTimeZone.SecondsFromGMT (sourceDate);
int interval = destinationGMTOffset - sourceGMTOffset;

var destinationDate = sourceDate.AddSeconds (interval);

var dateTime = new DateTime(2001, 1, 1, 0, 0,0).AddSeconds(destinationDate.SecondsSinceReferenceDate);

DescriptionLabel.Text = dateTime.ToString ("dd.MM.yyyy. HH:mm");
this.date = destinationDate;
milan.rancic
  • 219
  • 1
  • 8
  • 2
    This code adds unnecessary complexity. If you want to convert time to your local timezone simply use DateTime.ToLocalTime(). – Daria Trainor Sep 16 '13 at 02:36