I have an array containing the String objects with this format 22/04/2011.My question is that how can I change this string into the yyyy-mm-dd format
. I am using NSDateFormatter
but it's not开发者_如何学编程 working.
The real thing to know that when you convert a string date to NSDate using NSDateFormatter using any of the above mentioned methods, you will always get the NSDate object in Local TimeZone. So when coding the method for date conversion you need to consider this factor.
Hope this helps..
-:samfisher
Try this,
NSDateFormatter *formater = [[NSDateFormatter alloc] init];
[formater setDateFormat:@"dd/mm/yyyy"];
NSDate *date2 = [formater dateFromString:@"22/04/2011"];
[formater setDateFormat:@"yyyy-mm-dd"];
NSString *result = [formater stringFromDate:date2];
NSLog(@"Date - %@",result);
And For Reference - http://iosdevelopertips.com/cocoa/date-formatters-examples-take-3.html
Here how I do this ... This code works ... Try this
NSDate* currentDate = [NSDate date];
NSDateFormatter* df = [[NSDateFormatter alloc] init];
[df setDateFormat:@"yyyy-mm-dd"];
NSString* currentDateString = [df stringFromDate:currentDate];
[df release];
NSDateFormatter *formater = [[NSDateFormatter alloc] init];
[formater setDateFormat:@"dd/mm/yyyy"];
NSDate *date2 = [formater dateFromString:@"2010-11-17"];
[formater setDateFormat:@"yyyy-mm-dd"];
NSString *result = [formater stringFromDate:date2];
精彩评论