如何使用NSDate获取星期几并使用NSLog显示?
我想知道今天,比如今天。
Ex:NSLog(@“当天是:”,日)
回复:“这一天是:星期一”
答案 0 :(得分:21)
NSDateFormatter* day = [[NSDateFormatter alloc] init];
[day setDateFormat: @"EEEE"];
NSLog(@"the day is: %@", [day stringFromDate:[NSDate date]]);
答案 1 :(得分:16)
使用NSDateComponents:
static inline NSString *stringFromWeekday(int weekday)
{
static NSString *strings[] = {
@"Sunday",
@"Monday",
@"Tuesday",
@"Wednesday",
@"Thursday",
@"Friday",
@"Saturday",
};
return strings[weekday - 1];
}
NSDateComponents *components = [[NSCalendar currentCalendar] components:NSWeekdayCalendarUnit fromDate:[NSDate date]];
NSLog(@"%@", stringFromWeekday([components weekday]));
当然,这不尊重locales&这样
答案 2 :(得分:0)
NSLog(@"the day is: %@", [[NSDate date] descriptionWithCalendarFormat:@"%A" timeZone:nil locale:nil]);