1

I am trying to combine a NSDate (date) with a NSDate (time) into a new NSDate object, for whatever reason I seem to be losing 1 hour.

I have an array of formatted time objects that are 15 minutes between each one. I have a selectable date item, which referes to a NSDate object.

A user selects a formatted time object (I have a reference to the NSDate in a dictionary) and then when it comes to save; I do this;

if (self.selectedDate && self.selectedTimeObject)
    {
        // Combine selected date and selected time into a new NSDate object
        NSCalendar *cal = [NSCalendar currentCalendar];

        unsigned unitFlagsDate = NSYearCalendarUnit | NSMonthCalendarUnit |  NSDayCalendarUnit;
        unsigned unitFlagsTime = NSHourCalendarUnit | NSMinuteCalendarUnit |  NSSecondCalendarUnit;

        NSDateComponents *dateComponents = [cal components:unitFlagsDate fromDate:self.selectedDate];
        NSDateComponents *timeComponents = [cal components:unitFlagsTime fromDate:self.selectedTimeObject];

        [dateComponents setSecond:[timeComponents second]];
        [dateComponents setHour:[timeComponents hour]];
        [dateComponents setMinute:[timeComponents minute]];

        NSDate *combinedDate = [cal dateFromComponents:dateComponents];
        NSLog(@"targetDate = %@", combinedDate);
    }

In my example I choose 01:00; but for some reason I lose 1 hour depending on the date.

Logs;

targetDate = 2015-03-31 00:00:00 +0000
targetDate = 2015-03-30 00:00:00 +0000
targetDate = 2015-03-29 01:00:00 +0000
targetDate = 2015-03-28 01:00:00 +0000
targetDate = 2015-03-27 01:00:00 +0000
targetDate = 2015-03-31 00:00:00 +0000
targetDate = 2015-04-01 00:00:00 +0000
targetDate = 2015-04-02 00:00:00 +0000

Now in my logs, I noticed that anything before 28 is fine, but anything after that date loses 1 hour without explanation.

I have checked my array, I have checked my indexes, I have checked that the objects are valid time objects; however I am stumped -- why does it lose 1 hour?

Atilla Jax
  • 615
  • 5
  • 15

1 Answers1

3

The issue is most likely that you use NSLog(@"%@", date); to log your date. This will log times in UTC, i.e. +0000. If you are not living in a country that uses UTC (e.g. UK, Iceland, Ghana, Portugal, ...) these dates appear different from what you would expect. (See also: https://stackoverflow.com/a/4976950/457406)

If you create a date with NSDateComponents it uses your local timezone. Depending on your local timezone the same local time (e.g. 01:00), has different representations in UTC throughout the year. Also know as Daylight Savings Time.

In your experiments you saw that change at the end of March. Which is the time of the year when Daylight Savings Time starts in many countries. For example in Germany it starts on Sunday, March 29 at 2:00 in the morning. Which would explain the change in your logs on March 30 1:00.

To make a long story short, use something else to log your dates. For example:

NSLog(@"%@", [NSDateFormatter localizedStringFromDate: combinedDate dateStyle:NSDateFormatterShortStyle timeStyle:NSDateFormatterShortStyle]);
Community
  • 1
  • 1
Matthias Bauch
  • 89,811
  • 20
  • 225
  • 247