【问题标题】:String to NSDate字符串到 NSDate
【发布时间】:2011-07-12 14:55:22
【问题描述】:

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

谢谢,

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];

【问题讨论】:

  • 你需要进一步解释。你得到什么日期?

标签: iphone objective-c nsdate


【解决方案1】:

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

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

Unicode Date Format Patterns

【讨论】:

    【解决方案2】:

    用途:

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

    【讨论】:

      猜你喜欢
      • 2016-04-10
      • 1970-01-01
      • 2014-10-30
      • 2012-02-17
      • 2012-04-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多