2012-03-12 12 views
13

Tôi có mã này, nơi tôi đang cố gắng để có được ngày hiện tại và định dạng nó trong miền địa phương hiện tại.Làm cách nào để định dạng ngày hiện tại cho ngôn ngữ của người dùng?

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

Tôi biết dòng cuối cùng là sai, nhưng dường như không thể hiểu được ... "insertCmd" là NSString mà tôi đang xây dựng cho lệnh FMDB.

Trợ giúp sẽ được đánh giá rất nhiều hoặc trỏ đến "tài liệu" được mô tả.

Trả lời

29

Tôi sẽ không sử dụng setDateFormat trong trường hợp này, vì nó hạn chế trình định dạng ngày thành định dạng ngày cụ thể (doh!) - bạn muốn có định dạng động tùy thuộc vào ngôn ngữ của người dùng.

NSDateFormatter cung cấp cho bạn bộ ngày/giờ tích hợp kiểu mà bạn có thể chọn, ví dụ: NSDateFormatterMediumStyle, NSDateFormatterShortStyle v.v.

Vì vậy, những gì bạn cần làm là:

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

này sẽ cung cấp cho bạn với một chuỗi với một ngày dài vừa và một thời gian ngắn dài, tất cả tùy thuộc vào miền địa phương của người dùng. Thử nghiệm với các cài đặt và chọn bất cứ điều gì bạn thích.

Dưới đây là một danh sách các kiểu có sẵn: https://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSDateFormatter_Class/Reference/Reference.html#//apple_ref/c/tdef/NSDateFormatterStyle

3

Nếu bạn muốn ngày và thời gian cục bộ này sẽ cung cấp cho nó cho bạn:

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

Đoạn mã trên không cho bạn ngày và thời gian cục bộ .

5

Ngoài jiayow câu trả lời bạn có thể chỉ định tùy chỉnh của bạn 'mẫu' để có được phiên bản địa hoá:

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

Ví dụ sử dụng cho Mỹ/DE ngôn ngữ:

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