我如何从一个NSDatevariables获得Weekday和/或月份的名称?

好的。 我们遇到的问题是我们有NSString填充yyyyMMdd格式的date,我们想要做的是获得当前的工作日和月份的名称。 函数dagOmvandlare将datestring转换为工作日和月份。 然后函数调用viewDidload来命名我们所有的button标题从英语到瑞典的月份。

我们目前的解决scheme是这样的:

-(NSString *)dagOmvandlare:(id) suprDatum{ NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease]; dateFormatter.dateFormat = @"yyyyMMdd"; NSDate *date = [dateFormatter dateFromString:suprDatum]; NSString * monthString = [date descriptionWithCalendarFormat:@"%B"timeZone:nil locale:[[NSUserDefaults standardUserDefaults] dictionaryRepresentation]]; if ([monthString isEqualToString:@"January"]) { monthString = @"Januari"; } else if ([monthString isEqualToString:@"February"]) { monthString = @"Februari"; } else if ([monthString isEqualToString:@"May"]) { monthString = @"Maj"; } else if ([monthString isEqualToString:@"June"]) { monthString = @"Juni"; } else if ([monthString isEqualToString:@"July"]) { monthString = @"Juli"; } else if ([monthString isEqualToString:@"August"]) { monthString = @"Augusti"; } else if ([monthString isEqualToString:@"October"]) { monthString = @"Oktober"; } else if ([monthString isEqualToString:@"March"]) { monthString = @"Mars"; } return monthString; } - (void)viewDidLoad { [super viewDidLoad]; [button3 setTitle:(NSString *)[self dagOmvandlare:self.datum1] forState:UIControlStateNormal]; [button2 setTitle:(NSString *)[self dagOmvandlare:self.datum2] forState:UIControlStateNormal]; [bmanad1 setTitle:(NSString *)[self dagOmvandlare:self.datum3] forState:UIControlStateNormal]; } 

而我们到目前为止所知道的是,

 NSString * monthString = [date descriptionWithCalendarFormat:@"%B"timeZone:nil locale:[[NSUserDefaults standardUserDefaults] dictionaryRepresentation]]; 

不允许被苹果关于非公开API的指南使用。 所以主要的问题是,还有什么其他的方式来获得我们的date字符的星期几和月份名称,其中包含格式为yyyyMMdd的date?

没有必要手动转换为瑞典文字。 iPhone会为你做。 尝试这个:

 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); 

输出:

 month: Oktober day: Måndag 
 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); 

简单的Swift 3扩展:

 // Weekday extension Date { func dayOfWeek() -> String? { let dateFormatter = DateFormatter() dateFormatter.dateFormat = "EEEE" return dateFormatter.string(from: self).capitalized // or capitalized(with: locale) } } print(Date().monthName()!) // 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 

希望这可以帮助

 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]; 

这可能是有用的

另一个SO的问题可能会帮助你如何从今天的date使用NSDate查找星期几?

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

这个dayName将在用户的语言环境中可用。

正如苹果所说,创build(并让ARCconfiguration)格式化器是昂贵的,所以:

1)在你的文件中声明:

 private var localDateFormatter : DateFormatter? .. extension Date { 

2)使用懒惰的方法:

 if localDateFormatter == nil{ localDateFormatter = DateFormatter() let locale = Locale(identifier: "en_US_POSIX") ....} let newTime = localDateFormatter!.date(from: self) 

如果你在同一个class级有阿拉,静态/让会员会很好。 (我们不能在扩展名中使用variables… :(全局变得不好但是没办法…)

 - (void) getMonthFromDate:(NSString *) stringDate { NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init]; [dateFormat setDateFormat:@"yyyy-MM-dd"]; NSDate *date = [dateFormat dateFromString:stringDate]; NSCalendar* calendar = [NSCalendar currentCalendar]; NSDateComponents* components = [calendar components:NSCalendarUnitYear|NSCalendarUnitMonth|NSCalendarUnitDay fromDate:date]; // Get necessary date components NSLog(@"%lu", [components day]); NSLog(@"%lu %@",[components day],[[dateFormat monthSymbols] objectAtIndex:[components month]]); }