NSDateformatter setDateFormat according to currentLocale

前端 未结 6 2137
孤城傲影
孤城傲影 2021-02-14 12:17

I\'m going mad with, probably, a stupid problem.

I have 3 strings: year, month and day. I need to have a date in the right format based on currentLocale, so i.e. if curr

6条回答
  •  攒了一身酷
    2021-02-14 12:41

    If I understand your question, you want to set your NSDateFormatter to the locale of the user's device. For that you can just do something like this:

    NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
    [dateFormatter setDateStyle:NSDateFormatterMediumStyle];
    [dateFormatter setTimeStyle:NSDateFormatterNoStyle];
    [dateFormatter setLocale:[NSLocale currentLocale]];
    NSDate *date = [NSDate date];
    NSString *dateString = [dateFormatter stringFromDate:date];
    

提交回复
热议问题