关于NSDate、NSDateFormatter和NSCalendar的使用

(1)NSDate


  1>NSDate是系统的一个日期,时间类


  2>返回系统当前的日期时间


    +(id)date;


  3>返回未来secs秒后的日期,时间


    +(id)dateWithTimeIntervalSinceNow:(NSTimeInterval)secs;

 

  4>未来达不到的时间


    +(id)distantFuture;

 

  5>过去达不到的时间


    +(id)distantPast;


(2)NSDateFormatter

 

  NSDate *date=[NSDatedate];

    NSDateFormatter *formatter=[[NSDateFormatteralloc] init];

    [formatter setDateFormat:@"yyyy年M月dd日 hh:mm:ss"];

    

    NSLog(@"%@",[formatter stringFromDate:date]);


 (3) NSCalendar

 

  NSCalendar *calendar=[NSCalendarcurrentCalendar];

    

    NSUInteger componentsInt=NSYearCalendarUnit|NSMonthCalendarUnit|NSDayCalendarUnit|NSHourCalendarUnit

    |NSMinuteCalendarUnit|NSSecondCalendarUnit|NSWeekCalendarUnit|NSWeekdayCalendarUnit|NSWeekOfMonthCalendarUnit

    |NSWeekOfYearCalendarUnit|NSWeekdayOrdinalCalendarUnit;


    NSDateComponents *components=[calendar components:componentsInt fromDate:date];

    

    NSLog(@"%d",components.year);

    NSLog(@"%d", components.month);

    NSLog(@"%d",components.day);

    NSLog(@"%d", components.hour);

    NSLog(@"%d",components.minute);

    NSLog(@"%d", components.second);

    NSLog(@"%d",components.week);//一年中的第几周

    NSLog(@"%d", components.weekday);//周几,星期日为1以此类推,星期六为7

    NSLog(@"%d",components.weekOfMonth);//月中的第几周

    NSLog(@"%d", components.weekOfYear);//年中的第几周

    NSLog(@"%d", components.weekdayOrdinal);//第几次星期几

转载于:https://www.cnblogs.com/jiajiayouba/archive/2012/03/18/2404988.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值