Convert NSString of a date to an NSDate

后端 未结 3 1201
离开以前
离开以前 2020-12-01 21:23

This might be a silly question, but I can\'t seem to find the answer on here or in the documentation.

I want to convert an NSString such as @\"9/22/2010 3:45 PM\" t

3条回答
  •  情话喂你
    2020-12-01 22:04

    I was having the same problem, not sure why NSDateFormatter isn't working for me (iOS5 - Xcode 4.3.2) but this worked out for me:

    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
    [dateFormatter setLocale:[[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"]];
    [dateFormatter setDateFormat:@"dd-MM-yyyy"];
    NSString *dateString = @"05-06-2012";
    NSDate *date;
    NSError *error = nil; 
    if (![dateFormatter getObjectValue:&date forString:dateString range:nil error:&error]) {
        NSLog(@"Date '%@' could not be parsed: %@", dateString, error);
    }
    [dateFormatter release];
    

提交回复
热议问题