Day Name From NSDate?

前端 未结 6 1554
礼貌的吻别
礼貌的吻别 2020-12-15 16:29

I would like to show the name of day in my iPhone application and i don\'t found the solution. Thanks for help

相关标签:
6条回答
  • 2020-12-15 16:30

    I found it, the answer was :

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

    Thanks

    0 讨论(0)
  • 2020-12-15 16:32

    Look at -[NSCalendar components:fromDate:].

    A date by itself doesn't have a day, because it may have different days in different calendars (Gregorian, Chinese, etc.).

    EDIT: actually, sorry. That's what you would do to get the day and work with it programmatically. If you only want to display the day, look at NSDateFormatter.

    0 讨论(0)
  • 2020-12-15 16:41

    There a many great resources for date and time processing - this is one I've learnt from over on github.

    0 讨论(0)
  • 2020-12-15 16:46

    NSDate category

    + (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 will always be Monday! in case you need such behaviour

    0 讨论(0)
  • 2020-12-15 16:54
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"EEEE"];
    NSString *dayName = [dateFormatter stringFromDate:yourDate];
    [dateFormatter release];
    

    You get dayName in the locale of the user.

    (check Unicode standards for date formats samples)

    0 讨论(0)
  • 2020-12-15 16:54

    @Jilouc answer in Swift:

    let formatter = DateFormatter.init()
    formatter.dateFormat = "EEEE"
    let date = Date.init() //or any date
    let dayName = formatter.string(from: date)
    
    0 讨论(0)
提交回复
热议问题