iOS 获取周几

根据传入的日期,得到是周几,传入的格式是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

    . 以此类推

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值