How do I get weekday and/or name of month from a NSDate variable?

后端 未结 8 1645
春和景丽
春和景丽 2020-12-08 03:09

Alright. The problem we\'re having is that we have NSStrings filled with dates in the format of yyyyMMdd and what we want to do is to get the current weekday and name of mon

8条回答
  •  悲&欢浪女
    2020-12-08 03:24

    There is no need to manually convert to the Swedish words. iPhone will do it for you. Try this:

    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    dateFormatter.dateFormat = @"yyyyMMdd";
    NSDate *date = [dateFormatter dateFromString:@"20111010"];
    
    // set swedish locale
    dateFormatter.locale=[[NSLocale alloc] initWithLocaleIdentifier:@"sv_SE"];
    
    dateFormatter.dateFormat=@"MMMM";
    NSString *monthString = [[dateFormatter stringFromDate:date] capitalizedString];
    NSLog(@"month: %@", monthString);
    
    dateFormatter.dateFormat=@"EEEE";
    NSString *dayString = [[dateFormatter stringFromDate:date] capitalizedString];
    NSLog(@"day: %@", dayString);
    

    Output:

    month: Oktober
    day: Måndag
    

提交回复
热议问题