Get the Day from the date in iOS sdk

后端 未结 7 586
无人共我
无人共我 2021-01-16 12:36

In my app, want to get the day (i.e. Sunday, Monday,etc.) from the date.

My code is as follow:

NSDate *currentDate = [NSDat         


        
7条回答
  •  忘掉有多难
    2021-01-16 13:22

    I have made a small NSDate category that helps you find the weekday name from a date. It returns the localised string for the names of the weekdays. Have a look at it. You can use it if you want to.

    NKLocalizedWeekday+NSDate

    Basically, this is the code you need:

    NSCalendar *calendar = [NSCalendar currentCalendar];
    NSDateComponents *components = [calendar components:NSWeekdayCalendarUnit fromDate:[NSDate date]];
    int weekdayNumber = [components weekday];
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    NSString *weekdayString = [[formatter weekdaySymbols] objectAtIndex:weekdayNumber - 1];
    

提交回复
热议问题