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

后端 未结 8 1605
春和景丽
春和景丽 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:13
    NSString *strDate=@"20110407";
    NSDateFormatter *df=[[[NSDateFormatter alloc] init] autorelease];
    [df setDateFormat:@"yyyyMMdd"];
    NSDate *targetDate=[df dateFromString:strDate];
    [df setDateFormat:@"EEEE MMMM dd, yyyy"];
    NSString *s=[df stringFromDate:targetDate];
    
    NSLog(@"Date: %@", s);
    
    0 讨论(0)
  • 2020-12-08 03:16

    You can use (For Swift and Türkiye localization):

    dateFormatter.locale = NSLocale(localeIdentifier: "tr_TR") as Locale
    
    0 讨论(0)
  • 2020-12-08 03:18

    Hope this helps

    NSCalendar *calendar= [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
    NSCalendarUnit unitFlags = NSYearCalendarUnit | NSMonthCalendarUnit |  NSDayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit;
    NSDate *date = [NSDate date];
    NSDateComponents *dateComponents = [calendar components:unitFlags fromDate:date];
    
    NSInteger year = [dateComponents year];
    NSInteger month = [dateComponents month];
    NSInteger day = [dateComponents day];
    NSInteger hour = [dateComponents hour];
    NSInteger minute = [dateComponents minute];
    NSInteger second = [dateComponents second];
    
    [calendar release];
    

    This might be useful

    Another SO question which might help you How to find weekday from today's date using NSDate?

    0 讨论(0)
  • 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
    
    0 讨论(0)
  • 2020-12-08 03:26
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"EEEE"];
    NSString *dayName = [dateFormatter stringFromDate:Date];
    

    this dayName will be available in the locale of user.

    0 讨论(0)
  • 2020-12-08 03:28

    Simple Swift 3 extensions:

    // Weekday
    extension Date {
        func dayOfWeek() -> String? {
            let dateFormatter = DateFormatter()
            dateFormatter.dateFormat = "EEEE"
            return dateFormatter.string(from: self).capitalized
            // or capitalized(with: locale)
        }
    }
    
    print(Date().dayOfWeek()!) // Wednesday
    
    // Month Name
    extension Date {
        func monthName() -> String? {
            let dateFormatter = DateFormatter()
            dateFormatter.dateFormat = "MMMM"
            return dateFormatter.string(from: self).capitalized
            // or capitalized(with: locale)
        }
    }
    
    print(Date().monthName()!) // October
    
    0 讨论(0)
提交回复
热议问题