欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

日期转换为星期

程序员文章站 2022-06-07 17:59:51
...
NSArray *weekdays = [NSArray arrayWithObjects: [NSNull null], @"日", @"一", @"二", @"三", @"四", @"五", @"六", nil];

NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];

NSTimeZone *timeZone = [[NSTimeZone alloc] initWithName:@"Asia/Shanghai"];

[calendar setTimeZone: timeZone];
NSCalendarUnit calendarUnit = NSCalendarUnitWeekday;

NSDateComponents *theComponents = [calendar components:calendarUnit fromDate:date];

if(theComponents.weekday >= weekdays.count) {
return @”“;
}
return [weekdays objectAtIndex:theComponents.weekday];