Getting date from string in objective C - ios

I have to obtain date from a string, here the string will be in format "Monday 02,Dec 2013" i have to convert it to "2013-12-2" .I used the following code but I'm getting wrong output :
-(void)dateSelectedInPickerView:(NSString *)dateSelected{
NSDateFormatter *dateFormatterForGettingDate = [[NSDateFormatter alloc] init];
[dateFormatterForGettingDate setDateFormat:#"YYYY-MM-dd HH:mm:ss"];
// Parse the string representation of the date i.e Monday 2,Dec 2013
NSDate *dateFromStr = [dateFormatter dateFromString:dateSelected];
NSLog(#"date selected : %#",dateFromStr);
NSDateFormatter *tempFormatter=[[NSDateFormatter alloc]init];
[tempFormatter setDateFormat:#"YYYY-MM-dd"];
self.reservationDateSelected=[tempFormatter stringFromDate:dateFromStr];
NSLog(#"date Selected : %#",self.reservationDateSelected);
}

try this, it works for me with Monday 02,Dec 2013:
-(void)dateSelectedInPickerView:(NSString *)dateSelected{
NSDateFormatter *dateFormatterForGettingDate = [[NSDateFormatter alloc] init];
[dateFormatterForGettingDate setDateFormat:#"EEEE dd,MMM yyyy"];
[dateFormatterForGettingDate setLocale:[NSLocale localeWithLocaleIdentifier:#"EN"]];
// Parse the string representation of the date i.e Monday 2,Dec 2013
NSDate *dateFromStr = [dateFormatterForGettingDate dateFromString:dateSelected];
NSLog(#"date selected : %#", [dateFromStr descriptionWithLocale:[NSLocale currentLocale]]);
NSDateFormatter *tempFormatter=[[NSDateFormatter alloc]init];
[tempFormatter setDateFormat:#"yyyy-MM-dd"];
self.reservationDateSelected=[tempFormatter stringFromDate:dateFromStr];
NSLog(#"date Selected : %#",self.reservationDateSelected);
}
Read this, if want to know more about Date Format Patterns ;-)

1) You should use yyyy, not YYYY. YYYY stands for year in "Week of Year" based calendars - it may not always be the same value as calendar year. See the documentation.
2) Format to parse "Monday 02,Dec 2013" should be "EEEE dd,MMM yyyy"
EDIT
3) Forget to say about locale — device with non-english locale will fail to parse "Monday 02,Dec 2013" (because of "Monday" and "Dec") if you will not set the locale for the date formatter explicitly:
NSDateFormatter *dateFormatterForGettingDate = [[NSDateFormatter alloc] init];
dateFormatterForGettingDate.locale = [[NSLocale alloc] initWithLocaleIdentifier:#"en_US"];
[dateFormatterForGettingDate setDateFormat:#"EEEE dd,MMM yyyy"];
I think, this should work:
-(void)dateSelectedInPickerView:(NSString *)dateSelected{
NSDateFormatter *dateFormatterForGettingDate = [[NSDateFormatter alloc] init];
dateFormatterForGettingDate.locale = [[NSLocale alloc] initWithLocaleIdentifier:#"en_US"];
[dateFormatterForGettingDate setDateFormat:#"EEEE dd,MMM yyyy"];
// Parse the string representation of the date i.e Monday 2,Dec 2013
NSDate *dateFromStr = [dateFormatter dateFromString:dateSelected];
NSLog(#"date selected : %#",dateFromStr);
NSDateFormatter *tempFormatter=[[NSDateFormatter alloc]init];
[tempFormatter setDateFormat:#"yyyy-MM-dd"];
self.reservationDateSelected=[tempFormatter stringFromDate:dateFromStr];
NSLog(#"date Selected : %#",self.reservationDateSelected);
}

Related

NSDateFormatter returns nil while calling datefromstring method?

I'm trying to create a date object with the specified formatter but date formatter_datefromstring method returns nil. Please let me know with the clear documentation samples. The String am trying to parse is "2:00 AM PDT on September 24, 2017". Thanks in advance
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setTimeZone:[NSTimeZone systemTimeZone]];
[dateFormat setLocale:[NSLocale currentLocale]];
[dateFormat setDateFormat:#"h:mm a Z 'on' MMMM d, yyyy"];
NSLog(#"dateStr:==============> %#", dateStr);
NSDate *date = [dateFormat dateFromString:dateStr];
NSLog(#"Date:------------->%#", date);
return date;
You are using the wrong timezone specifier. Z is for timezones such as -0800. You need z for short timezone abbreviations like PDT.
Also, there is no reason to set the formatter's local to currentLocale and the timezone to systemTimeZone since those are the defaults. And the timezone of the formatter is irrelevant when the string you are parsing contains timezone information.
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"h:mm a z 'on' MMMM d, yyyy"];
NSLog(#"dateStr:==============> %#", dateStr);
NSDate *date = [dateFormat dateFromString:dateStr];
NSLog(#"Date:------------->%#", date);
return date;
However, since you are parsing a fixed format date string that is in English, you really should set the formatter's locale to the special locale of en_US_POSIX. This will ensure it handles the English month name no matter the user's locale.
[dateFormat setLocale:[NSLocale localeWithLocaleIdentifier:#"en_US_POSIX"]];
Can you check your dateStr date format and your given format same or not. If both are not same format you will get nil object. Try dateStr format in given below example.
NSString *dateStr = #"10:23 am Z on September 30, 2017";
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setTimeZone:[NSTimeZone systemTimeZone]];
[dateFormat setLocale:[NSLocale currentLocale]];
[dateFormat setDateFormat:#"h:mm a Z 'on' MMMM d, yyyy"];
NSLog(#"dateStr:==============> %#", dateStr);
NSDate *date = [dateFormat dateFromString:dateStr];
NSLog(#"Date:------------->%#", date);
In console you will get
2017-09-23 09:54:04.654597+0530 Date[4068:79926] dateStr:==============> 10:23 am Z on September 30, 2017
2017-09-23 09:54:04.657359+0530 Date[4068:79926] Date:------------->Sat Sep 30 15:53:00 2017
Use the below locale the avoid returning nil value.
NSLocale *locale = [[NSLocale alloc] initWithLocaleIdentifier:#"en_US"];
Vote my code if it is usefull

Convert NSString to NSDate and back to different format NSString

I need to take the NSString Dec 4, 2012, 12:33 PM and convert it to separate out the month, day, and year, so that I can have 3 different strings of 12, 04, and 2012.
I figure that I should convert the NSString to NSDate and then reformat the date to change out NSString, but am running into issues.
I have:
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"MMM dd, yyyy, hh:mm p"];
NSDate *dateFromString = [[NSDate alloc] init];
dateFromString = [dateFormatter dateFromString:substring];
NSLog(#"Date%#", dateFromString);
[dateFormatter release];
However, the date keeps coming back null.
The problem is with your locale I think.
You should try to print how your dateFormatter formats current date [NSDate new].
It works for me:
NSString* substring = #"Dec 12 2012 12:08 PM";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
dateFormatter.locale = [[NSLocale alloc] initWithLocaleIdentifier:#"en_US"];
[dateFormatter setDateFormat:#"MMM d yyyy h:mm a"]; // not 'p' but 'a'
NSDate *dateFromString = [dateFormatter dateFromString:substring];
Convert
[dateFormatter setDateFormat:#"MMM dd, yyyy, hh:mm p"];
to, Because MMM dd, yyyy, hh:mm p not a valid date formate
[dateFormatter setDateFormat:#"MMM dd, yyyy, hh:mm a"];
And
NSDate *dateFromString = [dateFormatter dateFromString:substring];
To get string again
NSLog(#"%#",[dateFormatter stringFromDate:dateFromString]);
for me NSLog is Dec 04, 2012, 12:33 PM
#import "NSString+Date.h"
#implementation NSString (Date)
+ (NSDate*)stringDateFromString:(NSString*)string;
{
NSString *dateString = #"01-02-2010";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
// this is imporant - we set our input date format to match our input string
// if format doesn't match you'll get nil from your string, so be careful
[dateFormatter setDateFormat:#"dd-MM-yyyy"];
NSDate *dateFromString = [[NSDate alloc] init];
// voila!
dateFromString = [dateFormatter dateFromString:dateString];
return dateFromString;
}
+(NSString*)StringFromDate :(NSDate*)date;
{
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"dd-MM-yyyy"];
NSString *stringDate = [dateFormatter stringFromDate:[NSDate date]];
NSLog(#"%#", stringDate);
return stringDate;
}
` #end

NSDate from String Conversion

Trying to convert the following string to an NSDate - I thought my DateFormatter was set correctly, but its not working:
NSString *dateToCheckString = #"Friday, Dec. 6, 2013 at 7:00 PM";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"eeee, MMM. dd, yyyy at hh a"];
NSDate *dateToCheckNSDate = [[NSDate alloc] init];
dateToCheckNSDate = [dateFormatter dateFromString:dateToCheckString];
// Log it out to see the result:
NSLog(#"Conversion yielded: '%#'", [dateToCheckNSDate description]);
The output I get is (null)
Any ideas?
No it's not correct. dd is a padded day (06 in your example) so that should be just d. Also "7:00" does not match hh (which is padded hours). You are looking for h:mm

Trying to change the date format on iOS

I parse a date from an xml file and store it in a string variable like this :
NSString *dateTex = [[stories objectAtIndex: storyIndex] objectForKey: #"date"];
When i try to print this variable on console i get :
the object value is:Fri, 01 Jun 2012 15:52:00 GMT
I am trying to change the format of the above date , to be stored again in a string variable but like this : dd/mm/yy.
I tried this code :
//changing dates format
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"EEE, d MMM YYYY HH:mm:ss Z"];
NSDate *date = [dateFormatter dateFromString:dateTex];
[dateFormatter release];
NSDateFormatter *dateFormatter2 = [[NSDateFormatter alloc] init];
[dateFormatter2 setDateFormat:#"dd/mm/yyyy"];
NSString *dateText = [dateFormatter2 stringFromDate:date];
[dateFormatter2 release];
NSLog(#"the object value is:%#",dateText);
But when i print the dateText variable on console i get :
the object value is:(null)
What am i doing wrong and the date is never stored in the variable?
Thanks!
Try this,(setDateFormat line is changed).
//changing dates format
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"EEE, dd MMM yyyy HH:mm:ss Z"]; // changed line in your code
NSDate *date = [dateFormatter dateFromString:dateTex];
[dateFormatter release];
NSDateFormatter *dateFormatter2 = [[NSDateFormatter alloc] init];
[dateFormatter2 setDateFormat:#"dd/MM/yyyy"]; // changed line in your code
NSString *dateText = [dateFormatter2 stringFromDate:date];
[dateFormatter2 release];
NSLog(#"the object value is:%#",dateText);
For Calender year use: 'yyyy' and for month use: 'MM'
Probably you parsing format is wrong. Check, if NSDate* date is filled correctly.
Perhaps you need another d, because you get the day with a leading zero.
Try: EEE, dd MMM YYYY HH:mm:ss Z.
Your date format for the first dateformatter is wrong. It should be
[dateFormatter setDateFormat:#"EEE, dd MMM YYYY HH:mm:ss Z"];
Hope this helps.

Date Formatter - String to Date 2012-02-05T00:00:00+00:00

I am attempting convert this string "2012-02-05T00:00:00+00:00" into a more attractive, nicely formatted string like "Tuesday March 5, 2012.
I have an example that I'm working from:
NSString *dateStr = #"20081122";
// Convert string to date object
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"yyyyMMdd"];
NSDate *date = [dateFormat dateFromString:dateStr];
// Convert date object to desired output format
[dateFormat setDateFormat:#"EEEE MMMM d, YYYY"];
dateStr = [dateFormat stringFromDate:date];
NSLog(#"date: %#", dateStr);
[dateFormat release];
How would I accommodate for the date format "2012-02-05T00:00:00+00:00" using similar code above?
Thank you.
You can do by using -
[dateFormat setDateFormat:#"yyyy-MM-ddThh:mm:ss z"];
When i see your time "2012-02-05T00:00:00+00:00", I think you are printing a "NSDate" into your console. In that time "2012-02-05" is a date and "T00:00:00+00:00" is a time. So when you need to use date like this,
NSString *dateStr = #"2012-02-05";
you have to format like this
[dateFormat setDateFormat:#"yyyy-MM-dd"];
If you want to convert the date component into as you liked try this
NSString *dateStr = #"2012-02-05";
// Convert string to date object
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"yyyy-MM-dd"];
NSDate *date = [dateFormat dateFromString:dateStr];
// Convert date object to desired output format
[dateFormat setDateFormat:#"EEEE MMMM d, YYYY"];
dateStr = [dateFormat stringFromDate:date];
NSLog(#"%#",dateStr);
You can even do like this to convert the current date into as you like.
// Convert date object to desired output format
[dateFormat setDateFormat:#"EEEE MMMM d, YYYY"];
dateStr = [dateFormat stringFromDate:[NSDate date]]; //for current date
NSLog(#"%#",dateStr);

Resources