字符串转 NSDate
我正在尝试将字符串转换为日期。使用了以下代码。但日期错误。
谢谢,
NSString *dateString = [NSString stringWithFormat:@"12-07-2011"];
//NSString *dateString = [NSString stringWithFormat:@"%d-%d-%d", selectedDate.day, selectedDate.month, selectedDate.year];
NSLog(@"%@",dateString);
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setLocale:[NSLocale currentLocale]];
[dateFormatter setTimeZone:[NSTimeZone localTimeZone]];
[dateFormatter setDateFormat:@"dd-mm-yyyy"];
NSDate* d = [dateFormatter dateFromString:dateString];
NSLog(@"%@",d);
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Selected Date" message: [NSString stringWithFormat:@"%@",d] delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alert show];
[alert release];
I am trying to convert string to date. have used the following code. But getting wrong date.
Thanks,
NSString *dateString = [NSString stringWithFormat:@"12-07-2011"];
//NSString *dateString = [NSString stringWithFormat:@"%d-%d-%d", selectedDate.day, selectedDate.month, selectedDate.year];
NSLog(@"%@",dateString);
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setLocale:[NSLocale currentLocale]];
[dateFormatter setTimeZone:[NSTimeZone localTimeZone]];
[dateFormatter setDateFormat:@"dd-mm-yyyy"];
NSDate* d = [dateFormatter dateFromString:dateString];
NSLog(@"%@",d);
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Selected Date" message: [NSString stringWithFormat:@"%@",d] delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alert show];
[alert release];
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
问题是你使用的是分钟而不是月份。
M
[dateFormatter setDateFormat:@"dd-MM-yyyy"];
Unicode 日期格式模式
The problem is you are using the minute and not the month.
M
[dateFormatter setDateFormat:@"dd-MM-yyyy"];
Unicode Date Format Patterns
使用:
Use: