NSDate formatter issue with Twitter JSON API - objective-c

I'm trying to format JSON response date value:
NSDateFormatter *df = [[NSDateFormatter alloc] init];
//Wed Dec 01 17:08:03 +0000 2010
[df setDateFormat:#"eee, MMM dd HH:mm:ss ZZZZ yyyy"];
NSDate *date = [df dateFromString:[twitter objectForKey:#"created_at"]];
[df setDateFormat:#"dd/MM/yyyy"];
NSString *dateStr = [df stringFromDate:date];
I follow instructions of this question:
Date/Time parsing in iOS: how to deal (or not deal) with timezones?
Actually, date value in JSON response is:
created_at":"Mon, 28 May 2012 11:20:29 +0000"
Date variable is nil when receiving value by [df dateFromString:[twitter objectForKey:#"created_at"]];
Many thanks

try this format if the month is abbreviated (Sept) :
[df setDateFormat:#"EEE, d MMM y HH:mm:ss Z"];;
and
[df setDateFormat:#"EEE, d MMMM y HH:mm:ss Z"];
if the month name is written out (September).
the full list of format specifiers.
I also had to change the locale
NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setLocale:[[[NSLocale alloc] initWithLocaleIdentifier:#"en_US"] autorelease]];
[df setDateFormat:#"EEE, d MMM y HH:mm:ss Z"];
NSDate *date = [df dateFromString:#"Mon, 28 May 2012 11:20:29 +0000"];
[df setDateFormat:#"dd/MM/yyyy"];
NSString *dateStr = [df stringFromDate:date];
NSLog(#"%#",dateStr);
output
28/05/2012

i found that the "created_at" attribute in the twitter Json response is in this format:
Thu Apr 19 10:07:29 +0000 2012
so i used this formatter :
[df setDateFormat:#"EEE MMM d HH:mm:ss Z y"];
it's working fine ;)

To validate the format you're using, create a string from a date:
NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setDateFormat:#"eee, MMM dd HH:mm:ss ZZZZ yyyy"];
NSLog(#"%#", [df stringFromDate: [NSDate date]];
Compare this with the date from your JSON response and you should be able to see where your format is wrong.
In Swift
let df = NSDateFormatter()
df.dateFormat = "eee, MMM dd HH:mm:ss ZZZZ yyyy"
let dateStr = df.stringFromDate(NSDate())
println("\(dateStr)")

Right formatter value is:
[df setDateFormat:#"eee, dd MMM yyyy HH:mm:ss ZZZZ"];
for:
created_at":"Mon, 28 May 2012 11:20:29 +0000"

"created_at" = "Sat Apr 05 06:31:57 +0000 2014";
Following Method Return How many time before twit aris.
-(NSString*)retrivePostTime:(NSString*)postDate{
NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setDateFormat:#"eee MMM dd HH:mm:ss ZZZZ yyyy"];
NSDate *userPostDate = [df dateFromString:postDate];
NSDate *currentDate = [NSDate date];
NSTimeInterval distanceBetweenDates = [currentDate timeIntervalSinceDate:userPostDate];
NSTimeInterval theTimeInterval = distanceBetweenDates;
// Get the system calendar
NSCalendar *sysCalendar = [NSCalendar currentCalendar];
// Create the NSDates
NSDate *date1 = [[NSDate alloc] init];
NSDate *date2 = [[NSDate alloc] initWithTimeInterval:theTimeInterval sinceDate:date1];
// Get conversion to months, days, hours, minutes
unsigned int unitFlags = NSHourCalendarUnit | NSMinuteCalendarUnit | NSDayCalendarUnit | NSMonthCalendarUnit | NSSecondCalendarUnit;
NSDateComponents *conversionInfo = [sysCalendar components:unitFlags fromDate:date1 toDate:date2 options:0];
NSString *returnDate;
if ([conversionInfo month] > 0) {
returnDate = [NSString stringWithFormat:#"%ldmth ago",(long)[conversionInfo month]];
}else if ([conversionInfo day] > 0){
returnDate = [NSString stringWithFormat:#"%ldd ago",(long)[conversionInfo day]];
}else if ([conversionInfo hour]>0){
returnDate = [NSString stringWithFormat:#"%ldh ago",(long)[conversionInfo hour]];
}else if ([conversionInfo minute]>0){
returnDate = [NSString stringWithFormat:#"%ldm ago",(long)[conversionInfo minute]];
}else{
returnDate = [NSString stringWithFormat:#"%lds ago",(long)[conversionInfo second]];
}
return returnDate;
}

Related

How to convert NSstring to Nsdate

I am getting response from service that
"created_at" = "Thu Jul 06 07:26:23 +0000 2017";
i want to convert the string into Nsdate format
i do not know how to do it
Try this:
NSString *dateString = #"Thu Jul 06 07:26:23 +0000 2017";
NSDate *date =[[NSDate alloc] init];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
dateFormatter.dateFormat = #"EEE MMM dd hh:mm:ss Z yyyy";
date = [ dateFormatter dateFromString:dateString];
Use this code
(source::http://nsdateformatter.com)
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"EEE MMM dd hh:mm:ss Z yyyy"];
NSDate *date = [dateFormat dateFromString:[NSString stringWithFormat:#"Thu Jul 06 07:26:23 +0000 2017"]];
NSLog(#"date is %# ",date);
NSDateFormatter *f = [[NSDateFormatter alloc] init];
[f setDateFormat:#"E MMM d HH:mm:ss Z yyyy"];
NSDate *date = [f dateFromString:#"Thu Jul 06 07:26:23 +0000 2017"];
Try this code:
NSString *dateString = #"2018-07-27 03:08:10";
NSDate *date =[[NSDate alloc] init];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
dateFormatter.dateFormat = #"yyyy-mm-dd hh:mm:";
date = [ dateFormatter dateFromString:dateString];
ObjectiveC implemetation
NSString *dateString = #"Thu Jul 06 07:26:23 +0000 2017";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
NSDate *date =[[NSDate alloc] init];
dateFormatter.dateFormat = #"EEE MMM DD hh:mm:ss Z yyyy";
date = [dateFormatter dateFromString: dateString]; //generates date from dateString
Swift implementation goes below
let dateString = "Thu Jul 06 07:26:23 +0000 2017"
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "EEE MMM DD hh:mm:ss Z yyyy"
dateFormatter.date(from: dateString) //"Jan 6, 2017, 12:56 PM"

Have troubles with NSDateFormatter while parsing

Here is the date sample: Wed, 13 May 2015 16:10:00 CEST
My formatter aint working
#"EEE, dd MMM yyyy HH:mm:ss vvvv"
What should i change?
UPD: full code
NSString* formattedDayWithString(NSString* date){
NSDateFormatter *inputFormatter = [[NSDateFormatter alloc] init];
NSLocale *locale = [NSLocale currentLocale];
NSString *localeId = [locale displayNameForKey:NSLocaleIdentifier
value:[locale localeIdentifier]];
SLog(#"%# date:%#", localeId, date);
[inputFormatter setLocale: locale];
[inputFormatter setDateFormat:#"EEE, dd MMM yyyy HH:mm:ss vvvv"];
NSDate *formattedDate = [inputFormatter dateFromString: date];
NSDateFormatter *outputFormatter = [[NSDateFormatter alloc]init];
[outputFormatter setDateFormat:#"d'.' MMMM yyyy"];
NSString* dateStr = [[NSString alloc] initWithFormat: #"%#",[outputFormatter stringFromDate:formattedDate]];
return dateStr;
}
"CEST" is a "short specific non-location format" and the corresponding
date field symbol is "z", not "v":
[inputFormatter setDateFormat:#"EEE, dd MMM yyyy HH:mm:ss z"];
In addition, you might have to set the date formatter locale explicitly to "en_GB"
as explained in Nsdateformatter + Timezone issue.

Formatting Date Using NSDateFormatter Not Working

I have this NSString that has a date and time that I want to format:
Jul 17, 2013 09:10 PM
I want to format it like this:
yyyy-MM-dd HH:mm
but it doesn't work.
This is the code I'm using:
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
formatter.locale = [[NSLocale alloc] initWithLocaleIdentifier:#"en_US"];
[formatter setDateFormat:#"MMM dd, yyyy hh:mm a"];
NSDate *_date = [formatter dateFromString:_dateToFormat];
NSLog(#"_date: %#", _date);
[formatter setDateFormat:#"yyyy-MM-dd HH:mm"];
NSString *_newDate = [formatter stringFromDate:_date];
NSLog(#"_newDate: %#", _newDate);
NSLog returns:
_date: 2013-07-17 04:10:00 +0000
_newDate: 2013-07-17 04:10:00 +0000
Am I missing something? After formatting the date, I want to subtract 45 minutes from the formatted date using the code:
_newDate = [_date dateByAddingTimeInterval:-60*45];
NSLog(#"Subtracted date: %#", _newDate);
But this isn't my worry right now since the first step isn't working, which is to format the date from NSString. Any idea why it isn't working?
Edit
Updated the typo on the code for dateByAddingInterval by changing _newDate with _date.
Since your dateString is in AM/PM format you will have to use
[formatter setDateFormat:#"MMM dd, yyyy hh:mm a"];
you are using HH and a in your dateFormat. HH means "hour in 24 hour format", and it looks like it takes precedence over a, the period (i.e. PM).
Use #"MMM dd, yyyy hh:mm a" with hh, which means "hour in 12 hour format" to convert your string into a NSDate.
NSString *_dateToFormat = #"Jul 17, 2013 09:10 PM"; // this is in local time zone! (mine is UTC+2)
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
formatter.locale = [[NSLocale alloc] initWithLocaleIdentifier:#"en_US"];
[formatter setDateFormat:#"MMM dd, yyyy HH:mm a"];
NSDate *_date = [formatter dateFromString:_dateToFormat];
NSLog(#"wrong _date: %#", _date);
[formatter setDateFormat:#"MMM dd, yyyy hh:mm a"];
_date = [formatter dateFromString:_dateToFormat];
NSLog(#"correct _date: %#", _date); // this is in UTC, not in local time zone!
NSLog(#"correct _date: %#", [_date descriptionWithLocale:[NSLocale currentLocale]]); // this should be in your local timezone
Output:
wrong _date: 2013-07-17 10:10:00 +0000
correct _date: 2013-07-17 19:10:00 +0000 (in my timezone: 21:10, or 09:10 PM)
correct _date: Wednesday, July 17, 2013, 9:10:00 PM Central European Summer Time
Keep in mind that printing a NSDate usually prints in UTC. So if your timezone is different the logged NSDate will not match your input date. It will be off by the offset between your timezone and UTC.
You can print [_date descriptionWithLocale:[NSLocale currentLocale]] to see the time in your local timezone.
But this is just when you NSLog the NSDate. The NSDate is still correct, it's just the printed output that seems to be wrong.
To conclude, your code should look like this:
NSString *_dateToFormat = #"Jul 17, 2013 09:10 PM";
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
formatter.locale = [[NSLocale alloc] initWithLocaleIdentifier:#"en_US"];
[formatter setDateFormat:#"MMM dd, yyyy hh:mm a"];
// create date from string
NSDate *_date = [formatter dateFromString:_dateToFormat];
// subtract 45 minutes
_date = [_date dateByAddingTimeInterval:-60*45];
[formatter setDateFormat:#"yyyy-MM-dd HH:mm"];
// turn date into string
NSString *_newDate = [formatter stringFromDate:_date];
NSLog(#"%#", _newDate);
Output: 2013-07-17 20:25
Use 2 different formatters, one for converting the string to a date and a second for converting your date back to a string. The first should use the en_US_POSIX locale and the second should probably use the system (default) locale (but if you want to force a particular format then you can use en_US_POSIX here too).
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"MMM dd, yyyy HH:mm a"];
NSDate *_date = [formatter dateFromString:_dateToFormat];
NSlog(#"Date: %#",_date);
(Posted answer on behalf of the question author).
I managed to make it work using this code with the appropriate output logs:
NSString *oldDate = #"Jul 17, 2013 09:10 PM";
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
formatter.locale = [[NSLocale alloc] initWithLocaleIdentifier:#"en_US"];
[formatter setDateFormat:#"MMM dd, yyyy hh:mm a"];
NSDate *_date = [formatter dateFromString:oldDate];
NSLog(#"_date: %#", _date);
_date = [_date dateByAddingTimeInterval:-60*45];
NSLog(#"_date2: %#", _date);
[formatter setDateFormat:#"yyyy-MM-dd HH:mm"];
NSString *_newDate2 = [formatter stringFromDate:_date];
NSLog(#"_newDate2: %#", _newDate2);
Output:
_date: 2013-07-17 13:10:00 +0000
_newDate: 2013-07-17 21:10
_date2: 2013-07-17 12:25:00 +0000
_newDate2: 2013-07-17 20:25

Change format of date

I got a NSString *string = #"2012-10-24 23:00:00 +0000";
And I want to convert that to a normal format 24 october 2012
I use the following code. But it keeps crashing. Can anyone help me?
NSDateFormatter *dateformat = [[NSDateFormatter alloc] init];
[dateformat setDateFormat:#"yyyy MM dd HH:mm:SS zzz"];
NSDate *dateNS = [dateformat dateFromString:date];
[dateformat setDateFormat:#"dd MM yyyy"];
date = [dateformat stringFromDate:dateNS];
[dateformat release]
It keeps crashing because it can't create the NSDate from the input date because the dateformat is incorrect. it missing the - between the date and the seconds are ss not SS.
Also you need a date formate with MMMM to get the months full name:
NSString *string = #"2012-10-24 23:00:00 +0000";
NSDateFormatter *dateformat = [[NSDateFormatter alloc] init];
[dateformat setDateFormat:#"yyyy-MM-dd HH:mm:ss zzz"];
NSDate *dateNS = [dateformat dateFromString:string];
[dateformat setDateFormat:#"dd MMMM yyyy"];
NSString *date = [dateformat stringFromDate:dateNS];
[dateformat release];

Help with date formats

What's the correct format to parse this string:
"Mon, 14 Mar 2011 15:36:00 GMT"
into a NSDate using NSDateFormatter?
NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
[dateFormatter setDateFormat:#"EEE, d MMM yyyy HH:mm:ss zzzz"];
NSDate *myDate = [dateFormatter dateFromString:strPubDate];
NSString *dateStr = [NSString stringWithString:#"Mon, 14 Mar 2011 15:36:00 GMT"];
NSDateFormatter *dateFormat = [[[NSDateFormatter alloc] init] autorelease];
[dateFormat setDateFormat:#"EEE, d MMM yyyy HH:mm:ss zzzz"];
NSDate *date = [dateFormat dateFromString:dateStr];