关于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=[[NSDateFormatterallocinit];

    [formatter setDateFormat:@"yyyyMdd 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);//第几次星期几

posted on 2012-03-18 19:43  iYiming  阅读(610)  评论(0编辑  收藏  举报

导航