根據傳入的日期,得到是周幾,傳入的格式是NSDate,直接上代碼:
+ (NSString*)weekdayStringFromDate:(NSDate*)inputDate {
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:inputDate];
return [weekdays objectAtIndex:theComponents.weekday];
}
默認週日爲1,以此類推.
1、 創建或初始化可用以下方法
`+ (id)currentCalendar;` 取得當前用戶的邏輯日曆(logical calendar)
`+ (id)autoupdatingCurrentCalendar;` 取得當前用戶的邏輯日曆(logical calendar), ......
- (id)initWithCalendarIdentifier:(NSString *)identifier;
初始化爲各種日曆。identifier的範圍可以是:
NSCalendarIdentifierGregorian 陽曆
NSCalendarIdentifierBuddhist 佛曆
NSCalendarIdentifierChinese 中國日曆
NSCalendarIdentifierCoptic 埃及日曆
NSCalendarIdentifierEthiopicAmeteMihret 埃塞俄比亞
NSCalendarIdentifierEthiopicAmeteAlem
NSCalendarIdentifierHebrew 希伯來日曆 NSCalendarIdentifierISO8601 ISO8601(但是現在還不可用)
NSCalendarIdentifierIndian 印度日曆
NSCalendarIdentifierIslamic 伊斯蘭教日曆
NSCalendarIdentifierIslamicCivil 伊斯蘭教民事日曆 NSCalendarIdentifierJapanese 日本日曆
NSCalendarIdentifierPersian 波斯 NSCalendarIdentifierRepublicOfChina 中華民國日曆(臺灣)
NSCalendarIdentifierIslamicTabular NSCalendarIdentifierIslamicUmmAlQura
2、使用前若有必要可以先做以下設定
- (void)setLocale:(NSLocale *)locale;
設置區域
- (void)setTimeZone:(NSTimeZone *)tz;
設置時區
- (void)setFirstWeekday:(NSUInteger)value;
設定每週的第一天從星期幾開始,比如:
. 如需設定從星期日開始,則value傳入1
. 如需設定從星期一開始,則value傳入2
. 以此類推