How to handle different date time formats using NSDateFormatter

后端 未结 4 1929
时光说笑
时光说笑 2020-12-15 05:18

I have a String with a datetime format: \"YYYY-MM-DD HH:MM:SS\".

I use this in my source code:

NSDateFormatter *formatter = [[[NSDateFormatter alloc]         


        
4条回答
  •  抹茶落季
    2020-12-15 06:02

    A date formatter can only handle one format at a time. You need to take this approach:

    NSDateFormatter *f = [[NSDateFormatter alloc] init];
    [f setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    NSDate *date = [f dateFromString:@"2010-01-10 13:55:15"];
    
    NSDateFormatter *f2 = [[NSDateFormatter alloc] init];
    [f2 setDateFormat:@"d. MMMM YYYY"];
    NSString *s = [f2 stringFromDate:date];
    

    s will now be "10. January 2010"

提交回复
热议问题