How to convert NSstring to Nsdate - objective-c

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"

Related

How do I represent it with date for "dd MMM yyyy", means "11 JUN 2019"?

I have date string like this "11 June, 2019 16:35:21". When I try to convert the string to NSDate it returns nil.
NSString str =#"11 June, 2019 16:35:21";
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat: #"dd MMM yyyy"];
NSDate *date = [formatter dateFromString:str];
NSString *string = [[formatter stringFromDate:date] uppercaseString];
Is there any other way to convert it? Or where I'm doing wrong?
use date format as like
NSString str =#"11 June, 2019 16:35:21";
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
NSLocale *locale = [[NSLocale alloc]
initWithLocaleIdentifier:#"en_US_POSIX"];
[formatter setLocale:locale];
// input dateformat #"11 June, 2019 16:35:21";
[formatter setDateFormat: #"dd MMMM, yyyy HH:mm:ss"];
NSDate *date = [formatter dateFromString:str];
// output dateformat for e.g #"11 Jun 2019
[formatter setDateFormat: #"dd MMM yyyy"];
NSString *string = [[formatter stringFromDate:date] uppercaseString];
The NSDateFormatter uses Unicode Technical Standard.
this site has a great documentation:
Date_Format_Patterns
e.g. "11 June, 2019 16:35:21" can be parsed with:
[dateFormatter setDateFormat:#"dd MMMM, yyyy HH:mm:ss"]
Karthik is right, first, you have to convert date string with #"dd MMMM, yyyy HH:mm:ss" format to NSDate, then convert it to your desired format #"dd MMM yyyy".
Hope so following code'll work.
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
NSLocale *locale = [[NSLocale alloc]
initWithLocaleIdentifier:#"en_US_POSIX"];
[formatter setLocale:locale];
[formatter setDateFormat: #"dd MMMM, yyyy HH:mm:ss"];
NSString *str = #"11 June, 2019 16:35:21";
NSDate *date = [formatter dateFromString:str];
formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat: #"dd MMM yyyy"];
NSString *string = [[formatter stringFromDate:date] uppercaseString];

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.

NSDate formatter issue with Twitter JSON API

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;
}

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];

return nil for dateFromString call of NSDateFormatter

I am getting nil returned for the date variable in the below code. I can't find any problem with the date format, can anyone help?
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"EEE MMM dd HH:mm:ss zzz yyyy"];
NSString *dateString = [[NSString alloc] initWithFormat:#"%#", #"Mon Apr 05 04:37:28 UTC 2010"];
NSDate *date = [formatter dateFromString:dateString];
Try
NSDate *date = [formatter dateFromString:#"Mon Apr 05 04:37:28 GMT 2010"];
or
NSDate *date = [formatter dateFromString:#"Mon Apr 05 04:37:28 +0000 2010"];