NSDateFormatter dateFromString format string issue - ios

I encountered a small problem today: I receive a couple of dates (NSString) from a web server. To use those dates correctly they need to be parsed into NSDate objects, for which I'm using the following code:
NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
formatter.dateFormat = #"dd.MM.yyy HH:mm:ss";
return [formatter dateFromString:dateString]
The dates I am receiving are in the following format e.g.: #"02.06.2015 13:31:24".
My problem is that the above code returns nil. I think the issue probably is that I don't have the correct format string, which I have not been able to get right..
Any help would be highly appreciated!

You are missing a 'y' in your format. It should be:
NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
formatter.dateFormat = #"dd.MM.yyyy HH:mm:ss";
return [formatter dateFromString:dateString]

Please try below code -
NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
formatter.dateFormat = #"dd.MM.yyyy HH:mm:ss";
return [formatter dateFromString:dateString]

-(NSDate *)getDateFromString:(NSString *)string
{
NSString * dateString = [NSString stringWithFormat: #"%#",string];
NSDateFormatter* dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"dd.MM.yyyy HH:mm:ss"];
NSDate* myDate = [dateFormatter dateFromString:dateString];
return myDate;
}
Call it wherever required :)
[self getDateFromString:#"13.07.2013 16:22:11"];

Related

How to format date using NSDateFormatter iOS?

I tried to use the NSDateFormatter to format a date that selected from a UIDatepicker. But the formatted result giving a nil.
-(NSString *)formatDateWithString:(NSString *)date{
NSDateFormatter* dateFormatter = [[NSDateFormatter alloc] init];
dateFormatter.dateFormat = #"yyyy-MMM-dd HH:mm:ss";
NSDate *formattedDate = [dateFormatter dateFromString:date];
return [dateFormatter stringFromDate:formattedDate];
}
date input is 2016-04-22 16:30:36 +0000(after selected from UIDatePicker). How may I fix this?
As mentioned in the comment you can get the NSDate directly from the UIDatePicker and pass that date directly in the formatter
NSDateFormatter * formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"yyyy-MMM-dd HH:mm:ss"];
NSString *dateString = [formatter stringFromDate:datePicker.date];//pass the date you get from UIDatePicker
If you want to convert the same way you have mentioned in the question you can try like this:
-(NSString *)formatDateWithString:(NSString *)date{
NSDateFormatter * formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"yyyy-MM-dd HH:mm:ss z"];
NSDate * convrtedDate = [formatter dateFromString:date];
[formatter setDateFormat:#"yyyy-MMM-dd HH:mm:ss"];
NSString *dateString = [formatter stringFromDate:convrtedDate];
return dateString;
}

Save a date from NSDate() without +0000 at the end in CoreData

I searched the posts but didn't find something I could use...
What I try to do is: I want to save a date in CoreData (as an NSDate) but without the timezone identifier like +0000. The reason for that is, I use the date later as sorting path / title in a TableView and it just looks not so nice with the +0000 at the end.
I can not convert the date from CoreData as I use the "titleForHeader" function together with the NSFetchedResultsController, so I have no chance to convert that beforehand...
Or is it even possible to save the NSDate with a format like "dd-MM-yyyy / HH:mm" but still as NSDate() ?
I want do all this in Swift!
Thanks for any help...
NSDate doesn't contain any timezone information, you need to save the NSDate in Core Data then use an NSDateFormatter to format it for display.
It's no possible to save a NSDate without that. Only if your are going to save as an NSString and in that case you can use something like this:
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat: #"yyyy-MM-dd HH:mm:ss"];
OR
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateStyle:NSDateFormatterShortStyle];
Then save the string as:
[dateFormatter stringFromDate:YOUR_NSDATE_VAR];
Cheers.
This is worked for me:
NSDate *date = [[NSUserDefaults standardUserDefaults] objectForKey:#"MaxDate"];
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"yyyy-MM-dd'T'HH:mm:ss"];
NSString *stringFromDate = [formatter stringFromDate:date];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
dateFormatter.dateFormat =#"yyyy-MM-dd'T'HH:mm:ss";
NSDate *dateFromString = [[NSDate alloc] init];
dateFromString = [dateFormatter dateFromString:stringFromDate];
dateFormatter.dateFormat = #"yyyy-MM-dd HH:mm:ss";
NSString *newDateString = [dateFormatter stringFromDate:dateFromString];
NSLog(#"%#", newDateString);

Can't parse date from string

I can't parse date from string via NSDateFormatter. The date string is #"2014-01-21T20:00:36+04:00". I am trying to use this format string: #"yyyy-MM-ddTHH:mm:ss" but it doesn't work. Please help me.
The full code is:
NSDateFormatter * dateFormatter = [[NSDateFormatter alloc] init];
dateFormatter.dateFormat = #"yyyy-MM-ddTHH:mm:ssZ";
NSDate * date = [dateFormatter dateFromString:dateString];
add Z at the end #"yyyy-MM-dd'T'HH:mm:ssZZZZZ" as you have timezone included
Following on from http://unicode.org/reports/tr35/tr35-6.html#Date_Format_Patterns
NSString *dateString = #"2014-01-21T20:00:36+04:00";
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
dateFormat.dateFormat = #"yyyy-MM-dd'T'HH:mm:ssZZZZZ";
NSDate *date = [dateFormat dateFromString:dateString];
NSLog(#"Parsed date is %#", date);
yields the following output which seems correct
Parsed date is 2014-01-21 16:00:36 +0000

Regular expression for date formats in iOS

In my project i need to check some format coming from server, So i want to write a regular expression to check the format.
This is the format "03/31/2013 08:00:00" and other format is "03/31/2003 08:00 AM/PM"
How can i check this date formats. Can any one help me.
Regards
Kiran
^(?:\d{2}\/){2}\d{4} \d{2}:\d{2}(?::\d{2}| (?:AM|PM))?$
Note that it won't check for incorrect values.
If you want to get NSDate from this string, you should use NSDateFormetter instead:
NSString * date = #"03/31/2013 08:00:00"; // source string
NSDateFormatter * date_formatter = [[NSDateFormatter alloc] init];
[date_formatter setDateFormat: #"MM/dd/yyyy HH:mm:ss"]; // your date format
NSDate * result = [date_formatter dateFromString: date]; // converting
NSLog (#"%#", [result description]); // log your date result
[date_format release];
Not a real answer to your question about regex but, if you want to get the date from the dateString received from server you can try a trial and error method with the dateFormat of NSDateFormatter.
NSString *dateFormat1 = #"MM/dd/yyyy HH:mm:ss";
NSString *dateFormat2 = #"MM/dd/yyyy hh:mm:ss a";
NSString *dateString = #"03/31/2013 08:00:00 AM";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
NSLocale *enUSPOSIXLocale = [[NSLocale alloc] initWithLocaleIdentifier:#"en_US_POSIX"];
[dateFormatter setLocale:enUSPOSIXLocale];
[dateFormatter setDateFormat:dateFormat1];
NSDate *date = [dateFormatter dateFromString:dateString];
if (!date) {
[dateFormatter setDateFormat:dateFormat2];
date = [dateFormatter dateFromString:dateString];
}
NSLog(#"Date : %#",date);

How to convert a RFC3339-String into a NSDate

i am trying to convert a RFC3339 formatted String Object to the corresponding NSDateObject :
A received String looks like this : 2012-10-29T00:00:00+01:00
I tried to use a NSDateFormatter but i dont know the correct specifiers ?
Any Ideas how to do this the right way ?
NSDateFormatter *dateFormat = [[NSDateFormatter alloc]init];
[dateFormat setDateFormat:#"yyyy-MM-ddThh:mm:ss???????"]; // ?????
NSDate *date = [dateFormat dateFromString:startAtString];
Inside one of my project I have this routine:
-(NSDate *)convertUTCStringToDate:(NSString *)utcDateString {
NSDateFormatter *df = [[NSDateFormatter alloc]init];
df.dateFormat = #"yyyy-MM-dd'T'HH:mm:ss";
return [df dateFromString:utcDateString];
}

Resources