2011-01-20 5 views

Odpowiedz

109
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; 
[dateFormatter setDateFormat:@"EEEE"]; 
NSString *dayName = [dateFormatter stringFromDate:yourDate]; 
[dateFormatter release]; 

Otrzymujesz dayName w ustawieniach regionalnych użytkownika.

(sprawdź Unicode standards dla próbek formaty daty)

+0

FYI różne wersje iOS/MacOS stosować różne wersje standardzie Unicode; Znalazłem starszą tablicę nich w https://developer.apple.com/library/content/documentation/Cocoa/Conceptual/DataFormatting/Articles/dfDateFormatting10_4.html – rakslice

+0

Otrzymuję skrót z tym: „Cz” – htafoya

3

Look at - [komponentów NSCalendar: fromdate:].

termin, sama w sobie nie mają dzień, ponieważ może to mieć różne dni w różnych kalendarzy (gregoriańskiego, chińskim, etc.).

EDIT: faktycznie, przepraszam. To, co zrobiłbyś, aby dostać dzień i pracować z nim programowo. Jeśli chcesz wyświetlić tylko dzień, spójrz na NSDateFormatter.

+0

zrobić, dzięki Ken –

0

istnieje wiele wspaniałych zasobów dla daty i czasu obróbki - jest to jeden nauczyłem znad na github.

14

znalazłem, odpowiedź brzmiała:

NSDate *now = [NSDate date]; 
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; 
[dateFormatter setDateFormat:@"EEEE"]; 
NSLog(@"%@",[dateFormatter stringFromDate:now]); 

Dzięki

kategoria
3

NSDate

+ (NSString *)dayNameWith0Monday:(NSInteger)index { 

    static NSDateFormatter * DateFormatter = nil; 
    if (DateFormatter == nil) { 
     DateFormatter = [[NSDateFormatter alloc] init]; 
     [DateFormatter setDateFormat:@"EEEE"]; 
     [DateFormatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]]; 
    } 

    NSDate * day = [NSDate dateWithTimeIntervalSince1970:((4 * 24 * 60 * 60) + (24 * 60 * 60 * index))]; 
    return [DateFormatter stringFromDate:day]; 
} 

0 zawsze będzie poniedziałek ! w przypadku potrzeby takiego zachowania

+0

jestem czegoś brakuje lub nie ma możliwości dla DateFormatter! = zero w metodzie klasy? – Andrespch

+0

można ewentualnie pominąć DateFormatter = nil; –

0

@Jilouc odpowiedź w SWIFT:

let formatter = DateFormatter.init() 
formatter.dateFormat = "EEEE" 
let date = Date.init() //or any date 
let dayName = formatter.string(from: date)