我想以2015年3月9日星期一的格式获取日期.但是下面的代码没有返回所需的日期格式.我想我使用的是错误的Formatter.这是代码:
NSString *dateString = @"09-03-2015"; NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; [dateFormatter setDateFormat:@"dd MMMM yyyy"]; NSDate *date = [[NSDate alloc] init]; date = [dateFormatter dateFromString:dateString]; NSLog(@"%@",[dateFormatter stringFromDate:date]);
解决方法
试试这个…
//Getting date from string NSString *dateString = @"09-03-2015"; NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; [dateFormatter setDateFormat:@"dd-MM-yyyy"]; NSDate *date = [[NSDate alloc] init]; date = [dateFormatter dateFromString:dateString]; // converting into our required date format [dateFormatter setDateFormat:@"EEEE,MMMM dd,yyyy"]; NSString *reqDateString = [dateFormatter stringFromDate:date]; NSLog(@"date is %@",reqDateString); LOG:2015-03-09 12:40:33.456 TestCode [1377:38775] date is Monday,March 09,2015