如何从 NSDate 变量获取工作日和/或月份名称?
好吧。我们遇到的问题是 NSString 填充了日期 格式为 yyyyMMdd,我们想要做的是获取当前工作日和月份名称。函数 dagOmvandlare 将日期字符串转换为工作日和月份。然后在 viewDidload 上调用该函数来命名我们所有的按钮标题,从英语到瑞典语月份。
我们当前的解决方案如下所示:
-(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 的指南不允许使用 。所以主要问题是,还有什么其他方法可以从包含 yyyyMMdd 格式的日期的日期字符串中获取工作日和月份名称?
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 month. The function dagOmvandlare converts the datestring to weekday and month. The function is then called on viewDidload to name all our button-titles from english to swedish months.
our current solution is looking like this:
-(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];
}
And what we've figured out so far is that the
NSString * monthString = [date descriptionWithCalendarFormat:@"%B"timeZone:nil
locale:[[NSUserDefaults standardUserDefaults] dictionaryRepresentation]];
is not allowed to be used by Apples guidelines regarding non-public api's. So the primary question is, what other way is there to get weekday and name of month out of our datestrings that contain dates with the format yyyyMMdd ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
无需手动转换为瑞典语单词。 iPhone 会为你做这件事。试试这个:
输出:
There is no need to manually convert to the Swedish words. iPhone will do it for you. Try this:
Output:
简单的 Swift 3 扩展:
Simple Swift 3 extensions:
希望这会有所帮助
这可能有用
另一个SO问题可能会帮助你如何使用 NSDate 从今天的日期查找工作日?
Hope this helps
This might be useful
Another SO question which might help you How to find weekday from today's date using NSDate?
this dayName 将在用户的区域设置中可用。
this dayName will be available in the locale of user.
正如苹果所说,创建(并让 ARC 处理)格式化程序的成本很高,因此:
1)在文件中声明:
2)使用惰性方法:
如果您在一个类中拥有所有内容,那么 static / let 成员会很好。 (我们不能在扩展中使用变量...:(全局变量似乎很糟糕,但没办法...)
As apple says, creating (and let ARC dispose) a formatter is expensive, so:
1) declare in your file:
2) use lazy approach:
if You have alla the stuff in one class, a static / let member would be nice. (we cannot use vars in extensions... :( globals seems bad but no way...)
您可以使用(对于 Swift 和 Türkiye 本地化):
You can use (For Swift and Türkiye localization):