iOS开发-通过NSDate获取年、月、日、星期
博客专区 > gwl_ 的博客 > 博客详情
iOS开发-通过NSDate获取年、月、日、星期
gwl_ 发表于11个月前
iOS开发-通过NSDate获取年、月、日、星期
  • 发表于 11个月前
  • 阅读 7
  • 收藏 0
  • 点赞 0
  • 评论 0

腾讯云 学生专属云服务套餐 10元起购>>>   

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

NSDate *now;

NSDateComponents *comps = [[NSDateComponents alloc] init];

NSInteger unitFlags = NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay | NSCalendarUnitWeekday | NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond;

now = [NSDate date];

comps = [calendar components:unitFlags fromDate:now];

NSLog(@"-----------year is %ld",(long)[comps year]);
NSLog(@"-----------month is %ld",(long)[comps month]);
NSLog(@"-----------day is %ld",(long)[comps day]);
NSLog(@"-----------weekday is %ld",(long)[comps weekday]);//在这里需要注意的是:星期日是数字1,星期一时数字2,以此类推...
/*
 NSCalendarUnitEra                = kCFCalendarUnitEra,
 NSCalendarUnitYear               = kCFCalendarUnitYear,
 NSCalendarUnitMonth              = kCFCalendarUnitMonth,
 NSCalendarUnitDay                = kCFCalendarUnitDay,
 NSCalendarUnitHour               = kCFCalendarUnitHour,
 NSCalendarUnitMinute             = kCFCalendarUnitMinute,
 NSCalendarUnitSecond             = kCFCalendarUnitSecond,
 NSCalendarUnitWeekday            = kCFCalendarUnitWeekday,
 NSCalendarUnitWeekdayOrdinal     = kCFCalendarUnitWeekdayOrdinal,
 NSCalendarUnitQuarter            NS_ENUM_AVAILABLE(10_6, 4_0) = kCFCalendarUnitQuarter,
 NSCalendarUnitWeekOfMonth        NS_ENUM_AVAILABLE(10_7, 5_0) = kCFCalendarUnitWeekOfMonth,
 NSCalendarUnitWeekOfYear         NS_ENUM_AVAILABLE(10_7, 5_0) = kCFCalendarUnitWeekOfYear,
 NSCalendarUnitYearForWeekOfYear  NS_ENUM_AVAILABLE(10_7, 5_0) = kCFCalendarUnitYearForWeekOfYear,
 NSCalendarUnitNanosecond         NS_ENUM_AVAILABLE(10_7, 5_0) = (1 << 15),
 NSCalendarUnitCalendar           NS_ENUM_AVAILABLE(10_7, 4_0) = (1 << 20),
 NSCalendarUnitTimeZone           NS_ENUM_AVAILABLE(10_7, 4_0) = (1 << 21),
 */

/*
 @property NSInteger era;
 @property NSInteger year;
 @property NSInteger month;
 @property NSInteger day;
 @property NSInteger hour;
 @property NSInteger minute;
 @property NSInteger second;
 @property NSInteger nanosecond NS_AVAILABLE(10_7, 5_0);
 @property NSInteger weekday;
 @property NSInteger weekdayOrdinal;
 @property NSInteger quarter NS_AVAILABLE(10_6, 4_0);
 @property NSInteger weekOfMonth NS_AVAILABLE(10_7, 5_0);
 @property NSInteger weekOfYear NS_AVAILABLE(10_7, 5_0);
 @property NSInteger yearForWeekOfYear NS_AVAILABLE(10_7, 5_0);
 */

 

共有 人打赏支持
粉丝 0
博文 6
码字总数 262944
评论 (0)
×
gwl_
如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!
* 金额(元)
¥1 ¥5 ¥10 ¥20 其他金额
打赏人
留言
* 支付类型
微信扫码支付
打赏金额:
已支付成功
打赏金额: