Create NSDate timezone issue - api

I am loading in dates from my web service, I'm sending dates in the format (GMT times): 02/11/11 10:56:09
I am creating an NSDate form this using NSDateFormatter as such:
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"dd/MM/yyyy HH:mm:ss"];
NSDate *journeyDate = [dateFormatter dateFromString:str];
[dateFormatter release];
This works great, after I'm comparing this to the current date to get relative time intervals.
The problem is when the phone is set up in a different timezone, when I load in the date from my api, and use the date formatter, what seems to be happening is the phone is assuming the date string is local time and it's converting it to GMT.
Example:
I load in a date with the time 10am from the api.
The phone is set to PDT.
The date formatter is creating an NSDate assuming that my date string with 10am, is actually relevant to the phone.
I end up with a date and time equal to 5pm, adding 10 hours.
I am trying to specify in my date formatter that the string is GMT, but I'm having trouble, I've tried the following, adding GMT to the format:
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"dd/MM/yyyy HH:mm:ss GMT"];
NSDate *journeyDate = [dateFormatter dateFromString:str];
[dateFormatter release];
This is not working.
Can anyone give any advice ?
Solution
Just a recap, I got it working with a terrible work around by appending GMT to the original string, and formatting that:
NSString * cheat = [NSString stringWithFormat:#"%# GMT", str];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"dd/MM/yyyy HH:mm:ss zzzz"];
NSDate *journeyDate = [dateFormatter dateFromString:cheat];
[dateFormatter release];
return journeyDate;
This was a kind of unstable hack, because if the string changed to include a timezone, it wouldn't work anymore. For anyone who needs to do as myself, the following is just a quick example on how to create an NSTimeZone.
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"dd/MM/yyyy HH:mm:ss"];
[dateFormatter setTimeZone:[NSTimeZone timeZoneWithName:#"UTC"]];
NSDate *journeyDate = [dateFormatter dateFromString:str];
[dateFormatter release];
return journeyDate;
Thanks for the quick help.

I suspect you just want to use NSDateFormatter.setTimeZone to force it to use UTC. You don't want to change the format string because presumably the string doesn't include the letters "GMT" - instead, you want to change which time zone the string is interpreted in, which is what setTimeZone will do.

You should use the setTimeZone method: http://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSDateFormatter_Class/Reference/Reference.html

Related

objective c convert string to UTC- the time is moving in error

I return a string from my database and value is '04/27/2016 1:16pm'. This value is already in UTC.
Now I want to convert that string to NSDATE, keeping it in UTC. When I try to convert string to date, the time is actually moving by 1 hour.
This is how I am doing it
NSString *tuploadtime = [tempDictionary valueForKey:#"uploadTime"];
//date conversions
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"MM-dd-yyyy HH:mm:ss a"];
[dateFormatter setTimeZone:[NSTimeZone timeZoneWithName:#"UTC"]];
NSDate *duploadtime = [[NSDate alloc] init];
duploadtime = [dateFormatter dateFromString:tuploadtime];
NSLog(#"tuploadtime=%#, duploadtime=%#", tuploadtime, duploadtime);
the result is coming back as 2016-04-27 12:16:01 UTC.
result is
2016-04-27 10:12:44.612 x[1558:48608] tuploadtime=4/27/2016 2:10:33 PM, duploadtime=2016-04-27 12:10:33 +0000
basically the time is moving back 1 hour but I want to keep it the same.
hope I am making sense
Proper String Format for Date is most Important.
There are some methods to have HOURS format as follow with their differences..
kk: will return 24 format Hour in (01-24) hours will (look like 01, 02..24).
HH will return 24 format Hour in (00-23) hours will(look like 00, 01..23).
hh will return 12 format Hour (look like 01, 02..12).
so you should use your code like
NSString *tuploadtime = [tempDictionary valueForKey:#"uploadTime"];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"MM-dd-yyyy hh:mm:ss a"];
[dateFormatter setTimeZone:[NSTimeZone timeZoneWithName:#"UTC"]];
NSDate *duploadtime = [[NSDate alloc] init];
duploadtime = [dateFormatter dateFromString:tuploadtime];
NSLog(#"tuploadtime=%#, duploadtime=%#", tuploadtime, duploadtime);
for More Date format refer this link
Your date format string is inconsistent:
[dateFormatter setDateFormat:#"MM-dd-yyyy HH:mm:ss a"];
HH means to use a 24-hour format. But then you also used a to indicate AM/PM. Using both is confusing the formatter and giving you off-by-one. You meant to use hh here.

NSDateFormatter: Converting "5/13/2012 6:05am" (US, EDT) to "13/05/2012 12:05" (IT, GMT+1)

I am retrieving a time value from a server. The format is:
"5/13/2012 6:05am"
It is not specified in the string but I know it is "EDT".
I need to:
1) Get rid of the am/pm thing.
2) Convert it from EDT (or other timezone) to the local GMT+/-x time.
For my timeZone (Italy, GMT+1) it should become:
"13/05/2012 12:05"
How can I do this with NSDateFormatter?
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setLocale: [NSLocale alloc] initWithLocaleIdentifier:#"en_US"]];
[dateFormatter setDateFormat:#"dd-MM-yyyy HH:mm"];
...?
Thanks
Nicola
First of all, you set your NSDateFormatters dateFormat to the source format. In your case:
[dateFormatter setDateFormat:#"M/d/yyyy h:mma"];
NSDate *myDate = [dateFormatter dateFromString:#"5/13/2012 6:05am"];
This creates a new instance of NSDate. Now you set the formatters dateFormat to your target format:
[dateFormatter setDateFormat:#"dd/MM/yyyy hh:mm"];
NSString *myString = [dateFormatter stringFromDate:myDate];
You could also use two date formatters, one for converting you'r string into NSDate instance and one to convert it back to the corrected NSString.
For further informations and the complete list of the current format specs refer to the unicode reference and the Apple Docs

Why isn't my time zone being saved into my NSDate?

I must initialize an NSDate object from NSString in objective-c. I do it like this:
NSString *dateString = [[webSentence child:#"DateTime"].text stringByReplacingOccurrencesOfString:#"T" withString:#" "];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"yyyy-mm-dd HH:mm:ss"];
[dateFormatter setTimeZone:[NSTimeZone timeZoneWithName:#"Europe/Budapest"]];
NSDate *date = [[NSDate alloc] init];
date = [dateFormatter dateFromString:dateString];
E.g: when I try it with string value #"2011-01-02 17:49:54" I get an NSDate 2011-01-02 16:49:54 +0000. As you can see there is a one hour difference between the two values. NSDate has a wrong value, it should be exactly the same I defined in my string in the timezone I set in dateFormatter. It seems it uses my date defined it string as UTC, even if I set its timezone to "Europe/Budapest". How can I fix this problem?
Thanks!
NSDate stores dates relative to a standard reference date. From the class docs:
"The sole primitive method of NSDate, timeIntervalSinceReferenceDate, provides the basis for all the other methods in the NSDate interface. This method returns a time value relative to an absolute reference dateā€”the first instant of 1 January 2001, GMT."
NSDate does not itself have any concept of time zones. So the NSDateFormatter did the right thing: it converted a date which you told it had a GMT offset (by specifying a time zone), and gave you a "normalized" NSDate for that date.
If you want to see the date represented in the Europe/Budapest time zone, either use your existing date formatter (-stringFromDate:) or the appropriate NSDate description method (e.g. -descriptionWithCalendarFormat:timeZone:locale:).
P.S.- You don't need an alloc/init at all in your code as written. In non-ARC that would be a leak.
P.P.S.- Your date format is incorrect and giving nonsensical results. I've gone ahead and cleaned up your code as follows (tested under ARC):
NSString *dateString = #"2011-09-02 17:49:54";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
NSTimeZone *tz = [NSTimeZone timeZoneWithName:#"Europe/Budapest"];
[dateFormatter setDateFormat:#"yyyy-MM-dd HH:mm:ss"];
[dateFormatter setTimeZone:tz];
NSDate *date = [dateFormatter dateFromString:dateString];
NSLog(#"%#", [dateFormatter stringFromDate:date]);
NSLog(#"%#", [date descriptionWithCalendarFormat:nil timeZone:tz locale:nil]);
Two things:
1) you have an error in your date format string. You should use MM for month, not mm (lowercase mm is for minutes)
2) after you create you NSDate object, you'll need to use the NSDateFormatter method stringFromDate: to generate a date string localized to a particular timezone. If you just do a straight NSLog() on the NSDate object it will show the date as GMT by default (GMT is one hour behind Budapest time)

Add +2 or -6 to the current GMT? (iphone)

i would like to create a tableView with different name of cities, and by clicking on each we can have the current time, i was thinking that i could put the GMT time and change it depending on the cell (the name of the city) we clicked : and thus, add +2 to the GMT hour, or -6 to the GMT. Do you think this could be possible?
I have this code for now, but i'm looking for some advices to create the "+2" or "-6" to this code : (this code is located in a method, updated every second to have the hour-minute-second updated)
NSDate *myDate = [NSDate date];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setTimeZone:[NSTimeZone timeZoneWithAbbreviation:#"GMT"]];
[dateFormatter setDateFormat:#"dd-MM-YYYY"];
NSString *GMTDateString = [dateFormatter stringFromDate: myDate];
[dateFormatter release];
Thanks for your help
So, you want do display current time in different time zones?
You can pass different time zones to dateFormatter and it'll convert date to that time zone and return it to you.
[dateFormatter setTimeZone:[NSTimeZone timeZoneWithAbbreviation:#"GMT"]];
[dateFormatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:3600*2]]; // GMT+2
[dateFormatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:-3600*6]]; // GMT-6
Use the date formatter for this. You just set the timezone and then you can get correct dates.
//-6 hours
[dateFormatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:-21600]];
//+2 hours
[dateFormatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:7200]];
If you would like to account for Daylight Savings use a named time zone instead of -6.

"setDateFormat" does not work with GMT?

i'm wondering why my setDateFormat does not work when i use it with a different timezone , here GMT :
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setTimeZone:[NSTimeZone timeZoneWithAbbreviation:#"GMT"]];
[dateFormatter setDateFormat:#"dd-MM-YYYY"]; //this doesn't work, nothing appears
//[dateFormatter setDateStyle:NSDateFormatterMediumStyle]; //this works
but if i use the default timeZone, it works :
NSDateFormatter *Dateformat = [[NSDateFormatter alloc]init];
[Dateformat setDateFormat:#"dd-MM-YYYY HH:mm:ss"]; //this works
Thanks for your help
Paul
Have you tried to NSLog a string by using this dateFormatter, its woking for me..when i make a string as-
NSString *dateStr = [dateFormatter stringFromDate:[NSDate date]];
NSLog(#"%#",dateStr);
and its give me this -
09-08-2011
Additionaly, you should probably be using: [dateFormatter setDateFormat:#"dd-MM-yyyy"] instead of [dateFormatter setDateFormat:#"dd-MM-YYYY"].
Using YYYY is a common mistake according to the apple documentation. It returns the year number of the year the week is in (according to ISO week numbering scheme). This can be off one year. Lowercase yyyy is normally the correct version.
See: iphone Get current year as string
(The comment of Anna Karenina)