繁体   English   中英

字符串到 NSDate

[英]String to 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];

问题是您使用的是分钟而不是月份。 M

[dateFormatter setDateFormat:@"dd-MM-yyyy"];

Unicode 日期格式模式

利用:

[dateFormatter setDateFormat:@"dd-MM-yyyy"];

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM