Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 ?

like image 885
Jens Bergvall Avatar asked Apr 08 '11 07:04

Jens Bergvall


People also ask

How do you extract the day of the week from a specific date?

Go to the Number tab in the Format Cells dialog box. Select Custom as the Category. Add dddd into the Type field for the full weekday name or ddd for the abbreviated weekday name. Press the OK button.

How do I print the day in Swift?

If you display a date with print(Date()) you'll get output like 2018-11-17 23:38:02 +0000 (The +0000 bit is the offset from UTC. An offset of 0000 from UTC means the date/time is expressed in the UTC time zone.)

How do I get current month and year in Swift?

year = yearsToAdd let futureDate = Calendar. current. date(byAdding: dateComponent, to: currentDate) print(currentDate) print(futureDate!) } func getCurrentDate()-> Date { var now = Date() var nowComponents = DateComponents() let calendar = Calendar.

How do I get todays date in Swift?

Get current time in “YYYY-MM–DD HH:MM:SS +TIMEZONE” format in Swift. This is the easiest way to show the current date-time.


2 Answers

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 
like image 93
Nick Moore Avatar answered Sep 28 '22 03:09

Nick Moore


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 
like image 26
brandonscript Avatar answered Sep 28 '22 01:09

brandonscript