Convert time to local time in Objective-C - objective-c

I'm having this issue where the correct time is passed but once it hits the [formatter setDateFormat:date] it comes out in a different time zone. Here is a snippet
NSDate* date = user.startDate;
NSDateFormatter *format = [[NSDateFormatter alloc] init];
NSLog(#"-------> (1) time is %#", date);
NSLog(#"-------> (2) time is %#", date);
[format setDateFormat:#"h:mm a"];
NSLog(#"-------> (3) time is %#", date);
NSLog(#"-------> (4) time is %#", [format stringFromDate:date]);
Here is the debugger output for the log messages
-------> (1) time is 2016-06-30 09:25:17 +0000
-------> (2) time is 2016-06-30 09:25:17 +0000
-------> (3) time is 2016-06-30 09:25:17 +0000
-------> (4) time is 2:25 AM
I'm expecting the output for (4) to be time is 9:25 AM

Your time zone is apparently UTC-7000.
NSLog prints NSDate objects always in UTC, NSDateFormatter considers the current time zone. The printed dates are actually the same.
If you really need all dates in UTC, set the time zone of the formatter.
format.timeZone = [NSTimeZone timeZoneForSecondsFromGMT:0];
By the way: There are built-in methods to check if a date is in today or yesterday.

Here's what is going on: the log of a date will always be GMT. (I don't think it makes sense for me to get into details as there is a great explanation here.) That means that log 1, 2 and 3 will be “wrong” (not in your timezone) and that should be expected.
If, for instance, you want to log the string as GMT, you can change the property timeZone of your date formatter.
Dealing with dates can be extremely hard. Please take the time to read if not all, some important parts of this document (fundamentals/basics).

Related

Objective-c NSDateFormatter timezone reverts to GMT

I am playing about with some timezone calculations and I have noticed quite an annoying error which is causing me problems.
If I create a date, use NSDateformatter to convert it to a certain time zone, retrieve that string, and then use dateformatter to convert the string back into a date object, it keeps reverting to my local GMT time.
Example
NSDate *date = [NSDate date];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"yyyy-MM-dd HH:mm:ss zzz"];
[dateFormatter setTimeZone:[NSTimeZone timeZoneWithAbbreviation:#"COT"]];
NSString *str = [dateFormatter stringFromDate:date];
NSLog(#"%#", str);
//prints 2015-02-04 10:33:45 GMT-5
NSDate *newDate = [dateFormatter dateFromString:str];
NSLog(#"%#", newDate);
//Prints 2015-02-04 15:33:45 +0000
Why does it keep reverting back to GMT? I need that date object to accurately reflect the time zone I have set the dateformatter to for some testing purposes, so this is quite a frustrating issue.
Any help would be much appreciated
I believe that the problem is that NSDate itself (i.e. the value you're logging at the end) doesn't have a time zone - it's just a moment in time. You're specifying the time zone *when formatting the value using stringFromDate*, and you're still using that when you *parse* the value back to anNSDate... but theNSDate` value itself doesn't remember the time zone.
To give a different example, imagine you had an IntegerFormatter for NSInteger, which let you say whether you wanted to format and parse in hex or decimal. You could format the decimal value 16 to 0x10, and then parse that value back... but the NSInteger wouldn't "remember" that it was parsed from hex. It's exactly the same here - the time zone plays a part in the parsing (at least when the value itself doesn't specify the time zone) but it isn't part of the result in itself.
I need that date object to accurately reflect the time zone
Then you need to keep the time zone separately alongside the NSDate, basically... (Looking at the documentation, it sounds like NSCalendarDate did what you want, but that's deprecated.)

DateTime conversion in iOS when device's timezone change

I get time in EDT from my server in format yyyy-MM-ddTHH:mm:ss for instance 2014-05-21T09:30:00. I convert these to NSDate and save in sqlite on iphone using code below
NSString *strDate = #"2014-05-21T09:30:00";
NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setTimeZone:[NSTimeZone timeZoneWithName:#"EDT"]];
[df setDateFormat:#"yyyy-MM-dd'T'HH:mm:ss"];
NSDate *date = [df dateFromString:strDate];
It works fine until I change the timezone on the device. If I change time on device time gets messed up. Any idea what am I doing wrong here?
Device's timezone is Eastern and I save 8:00AM
I change device's timezone to Pacific and time changes to 5:00AM
I have an update process that updates all datetime values again and it goes back to 8AM
I now change device's timezone to Eastern and time goes to 11AM
I run the update process again and time goes back to 8AM
How can I save these times so no matter which timezone users is in, they always see eastern time.
Thanks,
D.
NSDate itself doesn't know about timezone; it's just a simple wrapper around a double value, which represents the number of seconds since some fixed time (I think Jan 1 2001 or something) UTC.
What you need is a NSDateFormatter, similar to the one you use to parse the date, to transform the NSDate into an NSString using the EDT timezone.

UIDatepicker returns wrong value

my UIDatepicker returns a wrong value and I don´t understand why. See picture.
It shows 0 hours and 1 min but the label shows -1 hour.
I don´t find an time zone to set up. I expected that my app uses the time zone of the machine it´s on.
My action to fill the label:
- (IBAction)datePickerDateChanged:(id)sender {
[_timerOutput setText:[NSString stringWithFormat:#"%#", [_timePicker date]]];
}
I´m on a German system and I set the UIDatepicker to German but it still shows hours and mins.
Any idea what I´m doing wrong?
Ronald
This is the date and time in GMT wintertime (== UTC). use a NSDateFormatter to adjust it, it should use the default timezone by default. you also can set another.
try
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateStyle:NSDateFormatterMediumStyle];
[dateFormatter setTimeStyle:NSDateFormatterMediumStyle];
NSString *myDateString = [dateFormatter stringFromDate: [_timePicker date]];
NSLog(#"%#", myDateString);
Some theory:
A NSDate object does not represent what we call a date in every-days sense, like a day or a time of range, or what ever. It represents a single point in time. with sub-millisecond precision. And it does so by counting time intervals. From the beginning of a certain era. and by definition it is keeping UTC timezone as reference.
The internal counter will always be counting in UTC-context, it is your responsibility to display it correctly. But the NSDateFormatter is of huge help for that.
BTW: That your displayed time is a UTC time formatted one is also shown by +0000, as it has 00 hours and 00 minutes timezone offset from UTC.

6 hours being added to NSDate object when using NSDateFormatter

I am trying to format a date object and I am noticing on the string I am passing in; 6 hours is being added to my time. This seems to be associating my date time object to GMT.
My code:
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"yyyy-MM-dd h:mm:ss a"];
[formatter setTimeZone:[NSTimeZone systemTimeZone]];
NSDate *date = [formatter dateFromString:#"2012-02-01 03:38:12 AM"];
NSLog(#"%#", date);
The result is:
2012-02-01 09:38:12 +0000
I have tried this with and without the setTimeZone and it does not matter. Any ideas on why this is displaying as GMT time?
Thanks,
Flea
The date that your formatter creates is not associated with any timezone, but the description method of NSDate (which is what NSLog uses for the output) converts any date to UTC. You would have to use another (or the same) date formatter's stringFromDate: method to print it with a different time zone.
All NSDates are absolute times, meaning that 3:00 AM central time in the United States is 9 AM UTC. I suspect that your systemTimeZone is central time in the United States.
NSLog always shows times in UTC.
If you want to see, as a string, what the time is in your time zone, then you can use the same date formatter stringFromDate: method, and make the you set the time zone of the date formatter to that time zone.
NSLog date formatting is an annoyance because it leads to the kind of confusion you are experiencing.

Keep original time zone with NSDateFormatter

I have strings like 2011-01-19T20:30:00-5:00 and I'd like to parse them into an NSDate but I want to keep the original time zone.
NSDateFormatter *dateFormatter = [NSDateFormatter new];
[dateFormatter setDateFormat: #"yyyy-MM-dd'T'HH:mm:ssZ"];
NSLog(#"%#", [dateFormatter dateFromString: dateString]);
[dateFormatter release];
That snippet gives me 2011-01-20 02:30:00 +0100 which is also correct but I wish to keep the original time zone -0500 instead of my local time zone +0100 in the NSDate.
First of all, you should be aware that NSDate objects don't store anything related to their locales or timezones, and internally they're essentially represented as a number of seconds since the first instant of 1 January 2001, GMT.
If you are able to the timezone for the string in an NSTimezone object, just do the following before doing dateFromString:
[dateFormatter setTimeZone:timezoneForString];
and you'll be set.
If you're unable to get an NSTimezone and all you have is a string "2011-01-19T20:30:00-5:00", there isn't a very good way to get to an NSTimezone from the -5:00, since there isn't always an unambiguous way to get a timezone ID (e.g., "America/Los_Angeles") or timezone name (e.g., "Pacific Daylight Time") from an UTC offset. So you'd have to write your own code to manually extract the offset, store it, and add it to the time before displaying it.