NSDateFormatter, am I doing something wrong or is this a bug?

后端 未结 8 980
-上瘾入骨i
-上瘾入骨i 2020-11-30 06:18

I\'m trying to print out the date in a certain format:

NSDate *today = [[NSDate alloc] init];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]         


        
相关标签:
8条回答
  • 2020-11-30 07:17

    Using the code you posted on both the simulator and a phone with the 2.1 firmware and 24-hour time set to off, I never had an AM/PM appended to dateStr when I do:

    NSLog(@"%@", dateStr);
    

    Are you doing anything else with dateStr that you didn't post here? How are you checking the value?

    Follow up

    Try turning the am/pm setting on then off. I didn't have the problem either, until I did that. I am printing it out the same way you are.

    Okay, I see it when I do this also. It's gotta be a bug. I recommend you file a bug report and just check for and filter out the unwanted characters in the meantime.

    0 讨论(0)
  • 2020-11-30 07:18

    I think this is the solution .

    NSDateFormatter *df =[[NSDateFormatter alloc] init];
            [df setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
            NSLocale *usLocale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US"];
    
    [df setLocale: usLocale];
    
    [usLocale release];
    
     NSDate *documento_en_Linea =[[[NSDate alloc] init]autorelease];
    
    documento_en_Linea=[df dateFromString:@"2010-07-16 21:40:33"];
    
    [df release];
    
            NSLog(@"fdocumentoenLineaUTC:%@!",documento_en_Linea);
    
    
    //ouput  
         fdocumentoenLineaUTC:2010-07-16 09:40:33 p.m. -0500!
    
    0 讨论(0)
提交回复
热议问题