How do I get weekday and/or name of month from a N

2019-01-21 16:36发布

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 ?

7条回答
爱情/是我丢掉的垃圾
2楼-- · 2019-01-21 16:54
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"EEEE"];
NSString *dayName = [dateFormatter stringFromDate:Date];

this dayName will be available in the locale of user.

查看更多
Deceive 欺骗
3楼-- · 2019-01-21 16:55

Hope this helps

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

This might be useful

Another SO question which might help you How to find weekday from today's date using NSDate?

查看更多
Luminary・发光体
4楼-- · 2019-01-21 16:58

As apple says, creating (and let ARC dispose) a formatter is expensive, so:

1) declare in your file:

private var localDateFormatter : DateFormatter?

..
extension Date {

2) use lazy approach:

if localDateFormatter == nil{

        localDateFormatter = DateFormatter()

        let locale = Locale(identifier: "en_US_POSIX")

....}
    let newTime = localDateFormatter!.date(from: self)

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

查看更多
做自己的国王
5楼-- · 2019-01-21 17:02
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);
查看更多
地球回转人心会变
6楼-- · 2019-01-21 17:13

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
查看更多
贪生不怕死
7楼-- · 2019-01-21 17:13
- (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]]);

}
查看更多
登录 后发表回答