Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
319 views
in Technique[技术] by (71.8m points)

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

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 ?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...