---
title: "IOS NSDate日期操作总结"
author: "Davidream"
date: "2012-06-02T08:29:48Z"
tags: []
categories: ["移动开发"]
slug: "ios-nsdate"
draft: false
lastmod: "2012-06-02T08:29:48Z"
id: 1547099
url: "https://davidream.iteye.com/blog/1547099"
---

1 // 当前[时间](http://www.haogongju.net/tag/%E6%97%B6%E9%97%B4 "时间")创建NSDate

        NSDate \*myDate = \[NSDate date];

        NSLog(@"myDate = %@",myDate);

2 //从现在开始的24小时

        NSTime[In](http://www.haogongju.net/tag/in "In")terval secondsPerDay = 24\*60\*60;

        NSDate \*tomorrow = \[NSDate dateWithTimeIntervalSinceNow:secondsPerDay];

        NSLog(@"myDate = %@",tomorrow);

3//根据已有日期创建日期

         NSTimeInterval secondsPerDay1 = 24\*60\*60;

        NSDate \*now = \[NSDate date];

        NSDate \*yesterDay = \[now addTimeInterval:-secondsPerDay1];

        NSLog(@"yesterDay = %@",yesterDay);

 

 4//比较日期

        BOOL sameDate = \[now isEqualToDate:yesterDay];

        NSLog(@"sameDate = %lu",sameDate);

        4.1//[获取](http://www.haogongju.net/tag/%E8%8E%B7%E5%8F%96 "获取")较早的日期

        NSDate \*earlierDate = \[yesterDay earlierDate:now];

        NSLog(@"earlierDate  = %@",earlierDate);

        4.2//较晚的日期

        NSDate \*laterDate = \[yesterDay laterDate:now];

        NSLog(@"laterDate  = %@",laterDate);

 

        //两个日期之间相隔多少秒

        NSTimeInterval secondsBetweenDates= \[yesterDay timeIntervalSinceDate:now];

        NSLog(@"secondsBetweenDates=  %lf",secondsBetweenDates);

        //通过NSCALENDAR类来创建日期

        NSDate[Com](http://www.haogongju.net/tag/com "Com")p[one](http://www.haogongju.net/tag/one "one")nts \*comp = \[\[NSDateComponentsalloc]init];

        \[comp set[Mont](http://www.haogongju.net/tag/mont "Mont")h:06];

        \[comp setDay:01];

        \[comp setYear:2001];

        NSCalendar \*myCal = \[\[NSCalendaralloc]initWithCalendarIdentifier:NSGregorianCalendar];

        NSDate \*myDate1 = \[myCal dateFromComponents:comp];

        NSLog(@"myDate1 = %@",myDate1);

 

        //从已有日期获取日期

        unsigned units  = NSMonthCalendarUnit|NSDayCalendarUnit|NSYearCalendarUnit;

        NSDateComponents \*comp1 = \[myCal components:units fromDate:now];

        NSInteger month = \[comp1 month];

        NSInteger year = \[comp1 year];

        NSInteger day = \[comp1 day];

        //NSDate[For](http://www.haogongju.net/tag/for "For")matter实现日期的输出

        NSDateFormatter \*formatter = \[\[NSDateFormatteralloc]init];

        \[formatter setDateStyle:NSDateFormatterFullStyle];//直接输出的话是机器码

        //或者是手动设置[样式](http://www.haogongju.net/tag/%E6%A0%B7%E5%BC%8F "样式")\[formatter setDateFormat:@"yyyy-mm-dd"];

        NSString \*string = \[formatter stringFromDate:now];

        NSLog(@"string = %@",string);

        NSLog(@"formater = %@",formatter);

 

 

//获取日期格式对象

\- (NSDateFormatter \*)dateFormatter {

if (dateFormatter == nil) {

dateFormatter = \[\[NSDateFormatter alloc] init];

\[dateFormatter setDateStyle:NSDateFormatterMediumStyle];

\[dateFormatter setTimeStyle:NSDateFormatterNoStyle];

}

return dateFormatter;

}