2010-01-14 9 views
25

Pracuję z Objective C dla iPhone'a i mam NSDate, który chcę wyświetlić w pełnym stylu, ale bez roku. Teraz używam poniższego kodu, ale także pokazuje rok i nie chcę tego. Ponieważ chcę pokazać datę w odpowiednim formacie regionu, nie mogę po prostu usunąć roku na końcu, ponieważ w niektórych krajach rok nie będzie w końcu, ale w środku lub na początku.NSDate w pełnym stylu, ale bez roku

NSDate *testdate = [NSDate date]; 
NSDateFormatter *dateFormattertest = [[NSDateFormatter alloc] init]; 
[dateFormattertest setDateStyle:NSDateFormatterFullStyle]; 
[dateFormattertest setTimeStyle:NSDateFormatterNoStyle]; 
NSString *formattedDateString = [dateFormattertest stringFromDate:testdate]; 
NSLog(formattedDateString); 

In US this give me: 
Thursday, January 14, 2010 

In Sweden this give me: 
torsdag, 2010 januari 14 

Jakie jest rozwiązanie tego problemu? I kolejne pytanie, gdzie mogę znaleźć informacje na temat korzystania z setDateFormat z NSDateFormatter? Nie mogę znaleźć informacji o tym, co oznaczają różne litery w interfejsie API.

Odpowiedz

28

Wiem, że jest to dość stare pytanie, ale jest to jedyny, który znalazłem na SO, który mówił o tym samym problemie, który miałem.

Kluczem metoda, tutaj jest: dateFormatFromTemplate

Zamiast ustawiać styl, chcesz ustawić format jako takie:

NSDate *testdate = [NSDate date]; 

NSLocale *currentLocale = [NSLocale currentLocale]; 

// Set the date components you want 
NSString *dateComponents = @"EEEEMMMMd"; 

// The components will be reordered according to the locale 
NSString *dateFormat = [NSDateFormatter dateFormatFromTemplate:dateComponents options:0 locale:currentLocale]; 
NSLog(@"Date format for %@: %@", [currentLocale displayNameForKey:NSLocaleIdentifier value:[currentLocale localeIdentifier]], dateFormat); 

NSDateFormatter *dateFormatter =[[NSDateFormatter alloc] init]; 
[dateFormatter setDateFormat:dateFormat]; 

NSString *formattedDateString = [dateFormatter stringFromDate:testdate]; 
NSLog(formattedDateString); 

Specjalne podziękowania dla: http://oleb.net/blog/2011/11/working-with-date-and-time-in-cocoa-part-2/

+0

Wow, to działało jak czar. –

+0

To powinna być zaakceptowana odpowiedź. – CristiC

8

Took @Joss to świetna odpowiedź na rozszerzenie Swift:

extension NSDate { 
    func formattedFromCompenents(styleAttitude: NSDateFormatterStyle, year: Bool = true, month: Bool = true, day: Bool = true, hour: Bool = true, minute: Bool = true, second: Bool = true) -> String { 
     let long = styleAttitude == .LongStyle || styleAttitude == .FullStyle 
     var comps = "" 

     if year { comps += long ? "yyyy" : "yy" } 
     if month { comps += long ? "MMMM" : "MMM" } 
     if day { comps += long ? "dd" : "d" } 

     if hour { comps += long ? "HH" : "H" } 
     if minute { comps += long ? "mm" : "m" } 
     if second { comps += long ? "ss" : "s" } 

     let format = NSDateFormatter.dateFormatFromTemplate(comps, options: 0, locale: NSLocale.currentLocale()) 
     let formatter = NSDateFormatter() 
     formatter.dateFormat = format 
     return formatter.string(from: self) 
    } 
} 
-5

Oto szybka opcja pokazywania tylko Miesiąc Data na etykiecie:

let todaysDate: NSDate = NSDate() 
let formatter = NSDateFormatter() 
formatter.dateFormat = "MMMM dd" 
self.todayLabel.text = formatter.stringFromDate(todaysDate) 
+0

W pytaniu wyraźnie podano: "Chcę wyświetlić datę w odpowiednim formacie regionu". –

5

Swift 3

let template = "EEEEdMMM" 

    let format = DateFormatter.dateFormat(fromTemplate: template, options: 0, locale: NSLocale.current) 
    let formatter = DateFormatter() 
    formatter.dateFormat = format 

    let now = Date() 
    let whatYouWant = formatter.string(from: now) // ex: Sunday, Mar 5 

Gra z template do własnych potrzeb.

Dokumenty i przykłady here pomagają w określeniu pożądanego wzoru.

2

Swift 4

extension Date { 
    public func formattedFromComponents(styleAttitude: DateFormatter.Style, year: Bool = false, month: Bool = false, day: Bool = false, hour: Bool = false, minute: Bool = false, second: Bool = false, locale: Locale = Locale.current) -> String { 
     let long = styleAttitude == .long || styleAttitude == .full 
     let short = styleAttitude == .short 
     var comps = "" 

     if year { comps += long ? "yyyy" : "yy" } 
     if month { comps += long ? "MMMM" : (short ? "MM" : "MMM") } 
     if day { comps += long ? "dd" : "d" } 

     if hour { comps += long ? "HH" : "H" } 
     if minute { comps += long ? "mm" : "m" } 
     if second { comps += long ? "ss" : "s" } 
     let format = DateFormatter.dateFormat(fromTemplate: comps, options: 0, locale: locale) 
     let formatter = DateFormatter() 
     formatter.dateFormat = format 
     return formatter.string(from: self) 
    } 
}