gpt4 book ai didi

iphone - 字符串转 NSDate

转载 作者:行者123 更新时间:2023-12-03 20:21:06 27 4
gpt4 key购买 nike

我正在尝试将字符串转换为日期。使用了以下代码。但日期错误。

谢谢

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 Date Format Patterns

关于iphone - 字符串转 NSDate,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6666238/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com