I received date from Web service which is in GMT + 1 format (2010-02-25T20:16:50.387+05:30).
I want to convert in NSdate. I have no proper idea about date formatter.
Please suggest that how can i Change this nsstring value to current date format (Time zone)
Convert "2010-02-25T20:16" into NSDate using DateFormatter:
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
formatter.dateFormat = #"yyyyMMdd'T'HHmm";
NSDate *gmtDate = [formatter dateFromString:#"2010-02-25T20:16"];
Once you have the GMT date, you can add/substract the GMT-your time zone offset to it to get the current NSDate in your time zone. To get the offset between your timezone and GMT, you can use the following:
NSTimeInterval timeZoneOffset = [[NSTimeZone defaultTimeZone] secondsFromGMT];
NSDateFormatter is all about converting dates to and from string representations. Take a look at the docs for that.
Related
I want to convert my date Object to Unix time stamp.
// "**strSellTime**" has the string of my date
NSDateFormatter *dateFormatter=[NSDateFormatter new];
[dateFormatter setDateFormat:#"EEEE MMM yyyy-MM-dd hh:mm:ss a"];
NSDate *date=[dateFormatter dateFromString:strSellTime];
and wants to convert this date to UNIX time stamp like (1395382740) in this format.
Just use
[(NSDate*)date timeIntervalSince1970]
time_t unixTime = (time_t) [date timeIntervalSince1970];
was able to solve it by this
Any way to get timezone abbreviation(e.g UTC, EET) from NSDate? I'm getting NSDate from string, e.g 2012-08-26 02:54:50 +0200 and need to show timezone of this date.
Currently doing that:
NSDateFormatter *formatter = [[[NSDateFormatter alloc] init] autorelease];
[formatter setDateFormat: #"yyyy-MM-dd HH:mm:ssZZZ"];
NSDate *isoDate = [formatter dateFromString: isoTime.value];
NSInteger offset = [[NSTimeZone defaultTimeZone] secondsFromGMTForDate: isoDate];
NSTimeZone *timeZone = [NSTimeZone timeZoneForSecondsFromGMT: offset];
But I'm getting GMT+03:00 for timeZone abbreviation, but it should be EET. Any way to get it?
An NSDate is time zone independent; it doesn't inherently have a time zone. You therefore can't get a time zone from it.
secondsFromGMTForDate: returns the offset of that time zone (the default one, in your case) from GMT at the specified date. It's returning information about the NSTimeZone which may depend on the date (eg, if your time zone honours daylight savings), not about the date.
I want current date and time in PST. I used this code
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"yyyy-MM-dd HH:mm:ss zzz"];
[dateFormatter setTimeZone:[NSTimeZone timeZoneWithName:#"PST"]];
NSString *timeStamp = [dateFormatter stringFromDate:[NSDate date]];
NSLog(#"String:%#",timeStamp);
It returns correct date and time in PST in string form but I want NSDate in PST. So when I change NSString to NSDate like this:
NSDate *currentPST = [dateFormatter dateFromString:timeStamp];
NSLog(#"currentPST Date:%#",currentPST);
It returns date in GMT. I have done R&D but all in vain.Output is:
String:2011-05-18 22:28:54 PDT
currentPST Date:2011-05-19 05:28:54 +0000
Can anyone suggest a solution please.
Thanks in advance
In Cocoa, NSDate is an abstract representation of a date with no time zone information applied.
Whenever you print a NSDate object, it will print the date value corresponds to the default timezone(your device timezone). Your device timezone is GMT thats why you get the value like that. If you look into that deeply, both the time where same, but the timezone varies.
I am parsing the following string format: "29/09/2010 12:45:00" with the following code:
NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setDateFormat:#"dd/MM/yyyy HH:mm:ss"];
NSDate *dateTime = [[df dateFromString:dateAndTime]];
But the NSDate object then contains the following: 2010/09/29 11:45:00
Does anyone have any idea why it is taking off an hour?
Many thanks for any help.
What time zone are you in? Unless otherwise specified, NSDateFormatter assumes that the string it's parsing is in the GMT +0000 timezone. If you're in GMT -0100 (one hour west of GMT), then it's going to show the time as one hour off (since you're ignoring the time zone when printing).
I have a very strange date format coming to me via JSON. e.g. - "July, 18 2010 02:22:09"
These dates are always UTC. I'm parsing the date with NSDateFormatter, and setting the timeZone to UTC...
NSDateFormatter *inputFormatter = [[NSDateFormatter alloc] init];
[inputFormatter setTimeZone:[NSTimeZone timeZoneWithAbbreviation:#"UTC"]];
[inputFormatter setLocale:[[[NSLocale alloc] initWithLocaleIdentifier:#"en_US"] autorelease]];
[inputFormatter setDateFormat:#"MMMM, dd yyyy HH:mm:ss"];
NSDate *formatterDate = [inputFormatter dateFromString:dtStr];
However, the date when logged is appearing with the offset of my device...
"2010-07-18 02:22:09 -0600"
What am I doing wrong here?
I think this is because your NSDate isn't using the UTC timezone. The docs say this about the description method of NSDate:
A string representation of the receiver in the international format YYYY-MM-DD HH:MM:SS ±HHMM, where ±HHMM represents the time zone offset in hours and minutes from GMT (for example, “2001-03-24 10:45:32 +0600”).
So, you aren't really doing anything wrong. NSDate is behaving as expected. What are you trying to accomplish? Do you need the date in a string? Are you doing date comparison?