使用NSCalendar来获取当前日期,年月日星期

2017-02-10  本文已影响0人  turning_blue

最近有使用到日期获取,这里重新记一下算是复习

    NSCalendar  *calendar = [[NSCalendar  alloc]initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
    NSDateComponents  *comps = [[NSDateComponents alloc] init];
    NSInteger unitFlags = NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay | NSCalendarUnitWeekday |
    NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond;
    NSDate *now=[NSDate date];
    comps = [calendar components:unitFlags fromDate:now];
    
    NSLog(@"weekday is %d",[comps weekday]);//注意:星期日是数字1,星期一时数字2,以此类推。。。
    NSLog(@"month is %d",[comps month]);
    NSLog(@"day is %d",[comps day]);
    NSLog(@"weekdayOrdinal is %d",[comps weekdayOrdinal]);
上一篇下一篇

猜你喜欢

热点阅读