NSDate from String Conversion - ios

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

Related

Convert NSString to NSDate, adding time

I have an NSString that is 05/08/2014. I want to convert that to an NSDate. However, I also need to add in time, so that the resulting NSDate looks like this:
Thu, 8 May 2014 00:00:00 -0500
The time is not important, I just need it to show midnight at the designated timezone.
I have tried:
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"EEE, d MMM yyyy HH:mm:ss zzz"];
NSDate *dateFromString = [[NSDate alloc] init];
dateFromString = [dateFormatter dateFromString:textdate.text];
[dateFormatter release];
NSLog(#"%#", dateFromString);
But the date comes back as (null).
Your date format is wrong for the first conversion. What you need is first to convert from string to date from one format and form the new date into the new string format. Something like this:
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"MM/dd/yyyy"]; //convert the string into date (american time zone)
NSDate *theDate = [formatter dateFromString:textdate.text];
[formatter setDateFormat:#"EEE, d MMM yyyy HH:mm:ss ZZZ"];// as #Logan suggested
NSString *newDate = [formatter stringFromDate:theDate];
EEE, d MMM yyyy HH:mm:ss ZZZ
Your time zone code was lowercase instead of uppercase.
zzz corresponds to PDT
ZZZ corresponds to -0500
UTS 35

Getting date from string in objective C

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

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

NSDateFormatter wrong string after formatting

I receive a date through a string parameter, which is tempDateString, in a [day month year] format (for ex. 01 05 2005):
NSLog(#"tempdatestring %#", tempDateString);
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"dd MM YYYY"];
NSDate *dayDate = [dateFormatter dateFromString:tempDateString];
NSLog(#"daydate %#", dayDate);
My problem is, that the two logs don't match. The outputs are:
tempdatestring 04 10 2012
daydate 2011-12-24 22:00:00 +0000
What should I change at the date formatter's date format, to get the good date?
2 Problems
your format is wrong it is #"dd MM yyyy" case sensitive
Use timezone to get the correct value[GMT value]
NSString *tempDateString=#"04 10 2012" ;
NSLog(#"tempdatestring %#", tempDateString);
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];
[dateFormatter setDateFormat:#"dd MM yyyy"];
NSDate *dayDate = [dateFormatter dateFromString:tempDateString];
NSLog(#"daydate %#", dayDate);
Do this:
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"dd MM yyyy"];
NSDate *dayDate = [dateFormatter dateFromString:tempDateString];
NSLog(#"daydate %#", dayDate);
NSString *strDate = [dateFormatter stringFromDate:dayDate];
NSLog(#"strDate :%#",strDate);
When you use the %# format specifier, the return value of the -description method invoked on the provided object is used.
NSDate's -description method outputs its value in that specific way.
Your real problem though is that your date format string is incorrect - it should be dd MM yyyy.
I stuck this in a sample Xcode project:
NSString *s = #"04 11 2012";
NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setDateFormat:#"dd MM yyyy"];
NSDate *d = [df dateFromString:s];
NSDateComponents *c = [[NSCalendar currentCalendar] components:NSDayCalendarUnit | NSMonthCalendarUnit | NSYearCalendarUnit fromDate:d];
NSLog(#"%#", c);
It gave me the following output:
2012-10-04 01:53:24.320 dftest[59564:303] <NSDateComponents: 0x100113e70>
Calendar Year: 2012
Month: 11
Leap month: no
Day: 4
NSDateFormatter *form = [[NSDateFormatter alloc] init];
[form setDateFormat:#"dd MM yyyy"];
form.timeZone = [NSTimeZone timeZoneForSecondsFromGMT:-7200.0];
NSDate *dayDate = [form dateFromString:#"05 10 2012"];
NSLog(#"daydate %#", dayDate);
NSString *strDate = [form stringFromDate:dayDate];
NSLog(#"strDate %#",strDate);
Change date format to #"dd MM yyyy". After this, dateFromString may still parse the wrong date (in my case it was yesterday 21-00). To avoid this I've set TimeZone in my DateFormatter:
form.timeZone = [NSTimeZone timeZoneForSecondsFromGMT:-7200.0];
-7200.0 is my timezone, you should change this to yours ("0" sets to Greenwich). After this log looks like:
daydate 2012-10-05 02:00:00 +0000
strDate 05 10 2012

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.

Resources