如何格式化用户区域设置的当前日期?

时间:2012-03-12 23:49:36

标签: ios xcode4

我有这个代码,我正在尝试获取当前日期并将其格式化为当前语言环境。

NSDate *now = [NSDate date];  //  gets current date
NSString *sNow = [[NSString alloc] initWithFormat:@"%@",now];
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"mm-dd-yyyy"];
insertCmd = [insertCmd stringByAppendingString: formatter setDateFormat: @"MM.dd.yyyy"];

我知道最后一行是错的,但似乎无法弄明白......“insertCmd”是我正在为FMDB命令构建的NSString。

非常感谢帮助,或指向描述它的“doc”的指针。

4 个答案:

答案 0 :(得分:31)

在这种情况下我不会使用setDateFormat,因为它会将日期格式化程序限制为特定的日期格式(doh!) - 您需要一种动态格式,具体取决于用户的区域设置。

NSDateFormatter为您提供了一组可供选择的内置日期/时间样式,即NSDateFormatterMediumStyle,NSDateFormatterShortStyle等。

所以你应该做的是:

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

这将为您提供具有中等长度日期和短时长度的字符串,所有这些都取决于用户的区域设置。尝试设置并选择您喜欢的设置。

以下是可用样式列表:https://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSDateFormatter_Class/Reference/Reference.html#//apple_ref/c/tdef/NSDateFormatterStyle

答案 1 :(得分:6)

除了jiayow回答,您还可以指定自定义模板'获得本地化版本:

+ (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];
}

US / DE区域设置的示例用法:

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];

答案 2 :(得分:3)

如果您想要本地化的日期和时间,我们将为您提供:

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

上面的代码没有为您提供本地化的日期和时间。

答案 3 :(得分:1)

这是@JiaYou的Swift 5版本:

$(document).ready(function(){
    $('#tabs').tabs({
        swipeable: true,
        onShow: function(tab) {  
            ga('set', 'title', document.title);
            ga('set', 'page', window.location.pathname + window.location.search + window.location.hash);
            ga('send', 'pageview');
        },
    });
});

这里我只需要日期,因此我将func format(date: Date) -> String { let formatter = DateFormatter() formatter.dateStyle = .medium formatter.timeStyle = .none return formatter.string(from: date) } 表示为timeStyle