I want to calculate time span for twitter and facebook.
For twitter:-Tue Jul 19 11:08:46 +0000 2011
for facebook:-2011-07-18T15:25:09+0000
I want to convert it in to like,1 hrs ago,60 min ago,2 mint ago etc.
And how to compare these time values for Time sorting.
Kindly Provide any sample code or Any class reference link,So that i can do that.
I am in the process of doing something similar.
I would first convert the time given by Twitter and Facebook into a NSDate using NSDateFormatter's setDateFormat function. Then you can compare by using NSDate's timeSinceNow function.
In the case of Twitter, it might look like this…
NSDateFormatter *dateFM = [[NSDateFormatter alloc] init];
[dateFM setDateFormat:#"EEE MMM dd HH:mm:ss ZZZZ yyyy"]; //set the format that matches Twitter's result…
[dateFM setLocale:[[NSLocale alloc] initWithLocaleIdentifier:#"en_US"]];
[dateFM setTimeZone:[NSTimeZone timeZoneWithName:#"UTC"]];
NSDate *twitterDate = [dateFM dateFromString:CTADateString];
float secondsOfTwitterDateSinceNow = [twitterDate timeIntervalSinceNow];
The result (secondsOfTwitterDateSinceNow) is the number of seconds elapsed since now which you can divide however you want (/60 = minutes, ect…)
I have the following code:
NSDateFormatter * df = [[NSDateFormatter alloc] init];
[df setDateFormat:#"yyyy-MM-dd hh:mm:ss"];
date1 = [df dateFromString:#"2010-08-12 08:00:00"];
works as a charm, however when it is above 12:00:00, such as 23:00:00 the returned NSDate is null.
Am I being stupid?
use HH instead of hh to represent 24-hr format
[df setDateFormat:#"yyyy-MM-dd HH:mm:ss"];
You need to use a capital HH for military time, hh is for AM/PM
Change your hh to HH.
I assume the format string is not correct for your format.
Looking at the strings that can be supplied to setDateFormat, the Apple doc points to spec http://unicode.org/reports/tr35/tr35-10.html#Date_Format_Patterns.
hour
h 1..2 11 Hour [1-12].
H 1..2 13 Hour [0-23].
I have a string of the format Jan 25, 2011 3:17 AM. I need to convert it to NSDate.
I used NSDateFormatter with format #"MMM d, yyyy h:mm a". It Works well if iphone time is in 12 hr format, but returns nil if time is in 24 hr format. Can any one help me with this????
Capital H is used for 24 hour format. Don't 24 hour times usually exclude the AM/PM part? If so, your format string should be: #"MMM d yyyy H:mm".
Here's a reference for Unicode date format strings.
It's a bug in NSDateFormatter. You can work around it by manually setting a locale on the date formatter:
[dateFormatter setLocale:[[[NSLocale alloc] initWithLocaleIdentifier:#"en_GB"] autorelease]];
I have a string that is UTC and would like to convert it to an NSDate.
static NSDateFormatter* _twitter_dateFormatter;
[_twitter_dateFormatter setFormatterBehavior:NSDateFormatterBehaviorDefault];
[_twitter_dateFormatter setDateFormat:#"EEE MMM dd HH:mm:ss ZZZ yyyy"];
[_twitter_dateFormatter setLocale:_en_us_locale];
NSDate *d = [_twitter_dateFormatter dateFromString:sDate];
When I go through the debugger d is nil even though sDate is "2010-03-24T02:35:57Z"
Not exactly sure what I'm doing wrong.
Ditto on the need to alloc/init your NSDateFormatter object, but you have another problem too: your Date format string does not match the actual date you're giving it.
"EEE MMM dd HH:mm:ss ZZZ yyyy"
- vs -
"2010-03-24T02:35:57Z"
That format string would match something like:
"Wed Mar 24 00:07:33 -0400 2010"
See the unicode standard for the meaning of the date format string.
You aren't allocating or initialising your NSDateFormatter object.
Try something like this:
NSDateFormatter* _twitter_dateFormatter = [[NSDateFormatter alloc] init];
[_twitter_dateFormatter setFormatterBehavior:NSDateFormatterBehaviorDefault];
[_twitter_dateFormatter setDateFormat:#"EEE MMM dd HH:mm:ss ZZZ yyyy"];
[_twitter_dateFormatter setLocale:_en_us_locale];
NSDate *d = [_twitter_dateFormatter dateFromString:sDate];
[_twitter_dateFormatter release];
It's also unclear why you are declaring _twitter_dateFormatter as a static. If you are trying to avoid re-allocating it, make it an ivar of your class.
Apple gives source code for Parsing an RFC 3339 date-time. If using ARC, you'll may want to adjust that source by removing the calls to 'autorelease'.
FYI, RFC 3339 is a subset of ISO 8601 with one deviation (negative zero offset is allowed in RFC 3339). So Apple’s code is killing two birds with one stone, handling the so-called 'Internet-style' RFC 3339 datetimes as well as simple datetimes of ISO 8601.
I use a NSDateFormatter which works fine in the simulator, but I get a nil when I run it in the iPhone. I hardcoded the date to be sure of the format, but it fails anyway.
NSString *strPubDate = #"Fri, 8 May 2009 08:08:35 GMT";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setFormatterBehavior:NSDateFormatterBehavior10_4];
[dateFormatter setDateFormat:#"EEE, d MMM yyyy HH:mm:ss Z"];
NSDate *myDate = [dateFormatter dateFromString:strPubDate];
I tried with different region settings, languages etc. on the iPhone. Any idea what is going wrong?
This code seems to work correctly against the "GMT" tag.
NSString *strPubDate = #"Fri, 8 May 2009 08:08:35 GMT";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"EEE, d MMM yyyy HH:mm:ss zzzz"];
NSDate *myDate = [dateFormatter dateFromString:strPubDate];
RFC822 Date and Time specs use a few "zone" tags and I found the correct symbol to parse the "GMT" tag Format String for the iPhone NSDateFormatter
z~zzz: (Specific GMT Timezone Abbreviation)
zzzz: (Specific GMT Timezone Name)
Z: +0000 (RFC 822 Timezone
Thanks for all your help!
If you need to use a specific date format you might want to parse it "by hand" rather than using NSDateFormatter. Its behaviour does change depending on the locale, etc. and there are some bugs particularly when you have a timezone in your string.
Having said that, one option in finding what the problem is might be to use the getObjectValue:forString:range:error: method instead of dateFromString:. This way you get an NSError object that (in theory) would tell you what the problem is.
BTW, you don't need the NSDateFormatterBehavior10_4 line. iPhone OS only supports the 10.4+ options, though you won't get any errors if you use the "old" style in the Simulator.
I had also this problem recently (iOS5).
I needed to set also the locale of the NSDateFormatter to work on the device.
Without this, [dateFormatServer dateFromString:dateServer] was returning null.
NSString *dateServer = #"Fri, 8 May 2009 08:08:35 GMT";
NSDateFormatter *dateFormatServer = [[NSDateFormatter alloc] init];
[dateFormatServer setDateFormat:#"EEE, d MMM yyyy HH:mm:ss zzzz"];
NSLocale *locale = [[NSLocale alloc] initWithLocaleIdentifier:#"en_GB"];
[dateFormatServer setLocale:locale];
[locale release];
NSDate * date = [dateFormatServer dateFromString:dateServer];
Nothing stands out, but to help debug this you could try feeding NSDateFormatter a date object and seeing if the resulting string has any minor differences from the one you're trying to parse.
When I've parsed a date string with "GMT" at the end, I've used the "zzz" format, not "Z".
I second Manuel Spuhler's advice of manually parsing - not my favorite option, but Objective-C's options for that are way too complicated (and lacking in error reporting - anything wrong just spits nil, without a hint on the error).
One thing that worked for me is to use C's strptime to decouple the date, then reconstruct it as a NSDate object. For example, the code below tkes a string received as something like "Monday, 28-Sep-09 18:13:50 UTC" and converts it to a NSDate object adapting the UTC time for the local time:
struct tm time;
strptime([currentStringValue UTF8String], "%A, %d-%b-%y %H:%M:%S %z", &time);
NSDate* myDate = [NSDate dateWithString:
[NSString stringWithFormat:#"%04d-%02d-%02d %02d:%02d:%02d +0000",
time.tm_year+1900, time.tm_mon+1, time.tm_mday,
time.tm_hour, time.tm_min, time.tm_sec]
];
(could handle other zones by adding other struct tm parameters instead of the +0000 fixed time zone, see time.h entry on wikipedia for details):
strptime rescued me aswell.
struct tm time;
if (strptime([modDateString UTF8String], "%A, %d %b %Y %H:%M:%S", &time)) {
NSString *rDate=[NSString stringWithFormat:#"%04d-%02d-%02d %02d:%02d:%02d +0000",
time.tm_year+1900, time.tm_mon+1, time.tm_mday,
time.tm_hour, time.tm_min, time.tm_sec];
This code works with HTTP dates like Thu, 15 Apr 2010 13:53:56 GMT and Fri, 8 May 2009 08:08:35 GMT which was the original question