How do I format the current date for the user's locale?

后端 未结 4 735
难免孤独
难免孤独 2021-02-05 09:00

I have this code where I\'m trying to get the current date and format it in the current locale.

NSDate *now = [NSDate date];  //  gets current date
NSString *sNo         


        
相关标签:
4条回答
  • 2021-02-05 09:15

    I wouldn't use setDateFormat in this case, because it restricts the date formatter to a specific date format (doh!) - you want a dynamic format depending on the user's locale.

    NSDateFormatter provides you with a set of built-in date/time styles that you can choose from, i.e. NSDateFormatterMediumStyle, NSDateFormatterShortStyle and so on.

    So what you should do is:

    NSDate* now = [NSDate date];
    NSDateFormatter* df = [[NSDateFormatter alloc] init];
    [df setDateStyle:NSDateFormatterMediumStyle];
    [df setTimeStyle:NSDateFormatterShortStyle];
    NSString* myString = [df stringFromDate:now];
    

    This will provide you with a string with a medium-length date and a short-length time, all depending on the user's locale. Experiment with the settings and choose whichever you like.

    Here's a list of available styles: https://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSDateFormatter_Class/Reference/Reference.html#//apple_ref/c/tdef/NSDateFormatterStyle

    0 讨论(0)
  • 2021-02-05 09:17

    If you want the localized date and time this will give it to you:

        NSString *localizedDateTime = [NSDateFormatter localizedStringFromDate:[NSDate date] dateStyle:NSDateFormatterShortStyle timeStyle:NSDateFormatterShortStyle];
    

    The code above does not give you the localized date and time.

    0 讨论(0)
  • 2021-02-05 09:22

    In addition to jiayow answer you can specify your custom 'template' to get localized version:

    + (NSString *)formattedDate:(NSDate *)date usingTemplate:(NSString *)template {
        NSDateFormatter* formatter = [NSDateFormatter new];
    
        formatter.dateFormat = [NSDateFormatter dateFormatFromTemplate:template options:0 locale:formatter.locale];
    
        return [formatter stringFromDate:date];
    }
    

    Example usage for US/DE locale:

    NSLocale *enLocale = [NSLocale localeWithLocaleIdentifier:@"en_US"];
    NSLocale *deLocale = [NSLocale localeWithLocaleIdentifier:@"de"];
    
    // en_US:   MMM dd, yyyy
    formatter.dateFormat = [NSDateFormatter dateFormatFromTemplate:@"ddMMMyyyy" options:0 locale:enLocale];
    // de:      dd. MMM yyyy
    formatter.dateFormat = [NSDateFormatter dateFormatFromTemplate:@"ddMMMyyyy" options:0 locale:deLocale];
    
    // en_US:   MM/dd/yyyy
    formatter.dateFormat = [NSDateFormatter dateFormatFromTemplate:@"ddyyyyMM" options:0 locale:enLocale];
    // de:      dd.MM.yyyy
    formatter.dateFormat = [NSDateFormatter dateFormatFromTemplate:@"ddyyyyMM" options:0 locale:deLocale];
    
    // en_US    MM/dd
    formatter.dateFormat = [NSDateFormatter dateFormatFromTemplate:@"MMdd" options:0 locale:enLocale];
    // de:      dd.MM.
    formatter.dateFormat = [NSDateFormatter dateFormatFromTemplate:@"MMdd" options:0 locale:deLocale];
    
    0 讨论(0)
  • 2021-02-05 09:39

    Here's a Swift 5 version of @JiaYou's answer:

    func format(date: Date) -> String {
        let formatter = DateFormatter()
        formatter.dateStyle = .medium
        formatter.timeStyle = .none
        return formatter.string(from: date)
    }
    

    Here I need date only, so I indicate the timeStyle as none.

    0 讨论(0)
提交回复
热议问题