Convert String to NSDate always nil - objective-c

I want to covert from a NSString to NSDate object.
My string is : #"01/01/2013.
Here is the code to convert the string to NSDate:
- (NSDate*) formatDateTimeFromString: (NSString* ) string{
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"dd-MM-yyyy"];
NSDate* date= [dateFormatter dateFromString:string ];
return date;
}
I debug into the function and see that the date object is always nil. Did I have any mistake?

Your date formatter is wrong (might be hard to see at first)
Change
[dateFormatter setDateFormat:#"dd-MM-yyyy"];
to
[dateFormatter setDateFormat:#"dd/MM/yyyy"];

Related

Convert date in MM/dd/yyyy format in xcode?

I have a UIDatePicker and I m getting the selected date from it in yyyy-MM-dd format.Now I want to convert it to MM/dd/yyyy format ..How can i do it ?
NSDateFormatter *df=[[[NSDateFormatter alloc]init] autorelease];
df.dateFormat = #"yyyy-MM-dd";
NSArray *temp=[[NSString stringWithFormat:#"%#",[df stringFromDate:DatePicker.date]] componentsSeparatedByString:#""];
[dateString2 release];
dateString2=nil;
dateString2= [[NSString alloc]initWithString:[temp objectAtIndex:0]];
NSLog(#"%#",dateString2);
Im getting 2012-10-30 but I want 10/30/2012.
See Instruction or introduction with every line
NSString *str = #"2012-10-30"; /// here this is your date with format yyyy-MM-dd
NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease]; // here we create NSDateFormatter object for change the Format of date..
[dateFormatter setDateFormat:#"yyyy-MM-dd"]; //// here set format of date which is in your output date (means above str with format)
NSDate *date = [dateFormatter dateFromString: str]; // here you can fetch date from string with define format
dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
[dateFormatter setDateFormat:#"dd/MM/yyyy"];// here set format which you want...
NSString *convertedString = [dateFormatter stringFromDate:date]; //here convert date in NSString
NSLog(#"Converted String : %#",convertedString);
df.dateFormat = #"MM/dd/yyyy";
NSDateFormatter *formatter = [[[NSDateFormatter alloc] init] autorelease];
[formatter setDateFormat:#"MM/dd/yyyy"];
Just to see how it ends up:
NSDateFormatter* df = [[[NSDateFormatter alloc]init] autorelease];
df.dateFormat = #"MM/dd/yyyy";
NSString* dateString = [df stringFromDate:datePicker.date]; // Don't use leading caps on variables
NSLog(#"%#", dateString);
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"MM/dd/yyyy"];
The original accepted answer does a few things extra:
1) It allocates two NSDateFormatter instances which is not required. NSDateFormatter is expensive to create.
2) It is better to release date formatters explicitly when you are done with them rather than a deferred release using autorelease.
//get datepicker date (NSDate *)
NSDate *datePickerDate = [myDatePicker date];
//create a date formatter
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
//set its format as the required output format
[formatter setDateFormat:#"MM/dd/yyyy"];
//get the output date as string
NSString *outputDateString = [formatter stringFromDate:datePickerDate];
//release formatter
[formatter release];
- (void)showTime {
NSDate *now = [NSDate date];
NSDateFormatter *dateFormatter = [NSDateFormatter new];
[dateFormatter setDateFormat:#"HH:mm:ss"];
// you label
_time.text = [dateFormatter stringFromDate:now];
}
- (void)showDate {
NSDate *now = [NSDate date];
NSDateFormatter *dateFormatter = [NSDateFormatter new];
[dateFormatter setDateFormat:#"MM/dd/yyyy"];
// you label
_date.text = [dateFormatter stringFromDate:now];
}
- (void)viewDidLoad {
[super viewDidLoad];
self.showTime;
self.showDate;
}
Enjoy
NSString *str = #"2012-10-30"; /// here this is your date with format yyyy-MM-dd
NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease]; // here we create NSDateFormatter object for change the Format of date..
[dateFormatter setDateFormat:#"yyyy-MM-dd"]; //// here set format of date which is in your output date (means above str with format)
NSDate *date = [dateFormatter dateFromString: str]; // here you can fetch date from string with define format
dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
[dateFormatter setDateFormat:#"dd/MM/yyyy"];// here set format which you want...
NSString *convertedString = [dateFormatter stringFromDate:date]; here convert date in NSString
NSLog(#"Converted String : %#",convertedString);
I use a code with options NSISO8601DateFormatWithDashSeparatorInDate:
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
NSString * stringformat = [NSDateFormatter dateFormatFromTemplate:#"MMddy" options:NSISO8601DateFormatWithDashSeparatorInDate locale:[NSLocale systemLocale]];
[dateFormatter setDateFormat:stringformat];
NSDate *d = [NSDate date]; // or set your date
NSLog(#"date in format %#", [dateFormatter stringFromDate:d]); // date in format 2018-06-20

NSString to NSDate conversion for a specific format

I receive NSString(s) in the following format:
2013-05-18T17:37:06.9419518Z
How do I convert the date into a NSDate properly so I can compare it with another NSDate that contains the current date/time
This is what I have right now:
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"yyyy-MM-dd'T'HH:mm:ss.SSSSSSSZ"];
NSDate *dateFromString = [[NSDate alloc] init];
dateFromString = [dateFormatter dateFromString:extractString];
NSLog (#"dateFromString: %#", dateFromString);
[dateFormatter release];
[dateFromString release];
but I get a (null) output in the log out of it
thank you.
If the 'Z' is a constant at the end of your time, then you need to put it in single quotes in the date format.
[dateFormatter setDateFormat:#"yyyy-MM-dd'T'HH:mm:ss.SSSSSSS'Z'"];

How to convert a DD-MM-YYYY date format string into a YYYY-MM-DD date format string or into a NSDate object in Objective-C?

I have read date from XML which give me back string where the date is like DD-MM-YYYY. But when I want to add it to my core data database, SQLite sorts me that wrong so I have to convert it to date format or to string like: YYYY-MM-DD. But SQLite does not support either Date() or To_Date() functions. Can you help me?
You can use NSDateFormatter to format a date in Objective C.
Here's a quick example which might not do exactly what you want, but should hopefully give some pointers:
// Convert my dd-mm-yyyyy string into a date object
NSString *stringDate = #"01-02-2011";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"dd-MM-yyyy"];
NSDate *date = [dateFormatter dateFromString:stringDate];
// Convert my date object into yyyy-mm-dd string object
[dateFormatter setDateFormat:#"yyyy-MM-dd"];
NSString *formattedStringDate = [dateFormatter stringFromDate:date];
[dateFormatter release];
Hope this helps!
Nick.
Using Nick's tutorial and answer the solution is :
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"dd.MM.yyyy"];
NSDate *date = [dateFormatter dateFromString:#"01.02.1989"];
NSDateFormatter *dateFormatter1 = [[NSDateFormatter alloc] init];
// Convert my date object into yyyy-mm-dd string object
[dateFormatter1 setDateFormat:#"yyyy-MM-dd"];
NSString *formattedStringDate = [dateFormatter1 stringFromDate:date];
[dateFormatter release];
[dateFormatter1 release];
NSLog(formattedStringDate);
objektObrat.dateOfObrat = [[NSString alloc] initWithString:formattedStringDate];

Why doesn't NSDateFormatter return a NSString?

Why does this message not return a string:
NSDate *today = [NSDate date];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
NSString *formattedDate = (#"Date for today %#:", [dateFormatter stringFromDate:today]);
That expression does return an NSString, but without formatting. To format an NSString properly, you use the +stringWithFormat: method:
NSString* formattedDate = [NSString stringWithFormat:#"Date for today %#:",
[dateFormatter stringFromDate:today]];
The expression a, b is a "comma expression". It will evaluate a and b in sequence, and return the value of b. In you case, it will return the string of date only.
Most of the case, you do not want to use a comma expression.
Also, the dateFormatter needs to be configured before using. For instance,
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setTimeStyle:NSDateFormatterNoStyle];
[dateFormatter setDateStyle:NSDateFormatterMediumStyle];
...
... [dateFormatter stringFromDate:today] ...
...
[dateFormatter release];

Why does my NSDateFormatter return nil when parsing?

my code is like this
NSString *tempDate = [NSString stringWithString:tempReviewData.pubDate];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateStyle:NSDateFormatterLongStyle];
[dateFormatter setTimeStyle:NSDateFormatterNoStyle];
[dateFormatter setDateFormat:#"HH:mm a"];
NSDate *newDate = [dateFormatter dateFromString:tempReviewData.pubDate];
My newDate is getting nil at this point i dont know why
It seems to work for me but it depends on the format of tempReviewData.pubDate.
When I use invalid format, like #"6:30 M", I get null as well.
This is working:
NSString *tempDate = [NSString stringWithString:#"6:30 PM"];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateStyle:NSDateFormatterLongStyle];
[dateFormatter setTimeStyle:NSDateFormatterNoStyle];
[dateFormatter setDateFormat:#"HH:mm a"];
NSDate * newDate = [dateFormatter dateFromString:tempDate];
NSString * str = [dateFormatter stringFromDate:[NSDate date]];
NSLog(#"date: %#", newDate);
NSLog(#"str: %#", str);
Output:
2010-03-08 22:36:57.904 x[4340:903] date: 1970-01-01 12:30:00 +1000
2010-03-08 22:36:57.905 x[4340:903] str: 22:36 PM
NSDate *newDate = [dateFormatter dateFromString:tempReviewData.pubDate];
Does pubDate return an NSString, or an NSDate?
If it returns a string, then you should rename that property to clearly indicate that.
If it returns a date (NSDate), then trying to parse it as a string will not work, since it is not a string; moreover, you can cut out all this formatter code, since you already have the date object you're after.
It seems the NSDateFormatter has gotten very picky.
-(void)dateFormatterTests {
NSDateFormatter *formatter;
formatter = [[NSDateFormatter alloc] init];
#ifdef WORKS
[formatter setDateFormat:#"yyyy-MM-dd"];
#elif defined(ALSO_WORKS)
[formatter setDateFormat:#"yyyy MM dd"];
[formatter setLenient:YES];
#else // DOESN'T WORK
[formatter setDateFormat:#"yyyy MM dd"];
#endif
// Works per comments above
NSLog(#"dFS: %#", [formatter dateFromString:#"2010-01-13"]);
// Never works with any of the above formats
NSLog(#"dFS: %#", [formatter dateFromString:#"2010-01-13 22:00"]);
[formatter release]; formatter = nil;
}