如何从NSDate变量获取工作日和/或月份名称? [英] How do I get weekday and/or name of month from a NSDate variable?

查看:107
本文介绍了如何从NSDate变量获取工作日和/或月份名称?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好的。我们遇到的问题是我们的NSStrings以yyyyMMdd的格式填充日期
,我们想要做的是获取当前的工作日和月份名称。函数dagOmvandlare将日期字符串转换为工作日和月份。然后在viewDidload上调用该函数,将所有按钮标题命名为英语到瑞典语。

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

不允许Apples指南使用非公开API。所以主要的问题是,有什么其他方式可以获得工作日和月份名称,其中包含日期格式为yyyyMMdd的日期字符串?

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 ?

推荐答案

无需手动转换为瑞典语单词。 iPhone会为你做的。试试这个:

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

输出:

month: Oktober
day: Måndag

这篇关于如何从NSDate变量获取工作日和/或月份名称?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆