畅想网络网站建设推广,网站排名不稳定怎么办,刚做网站做多用户还是单用户,可以免费制作网页的网站目录 一、创建日期
1.获取当前时间
2.当前时间指定秒数之后/前的时间
3.指定日期之后/后的时间
4.2001年之后/前指定秒数的时间
5.1970年之后/后指定秒数的时间
二、初始化日期
1.init
2.时间间指定秒数的时间
3.指定时间指定秒数之前/后的时间
4.2001年指定秒数之后…
目录 一、创建日期
1.获取当前时间
2.当前时间指定秒数之后/前的时间
3.指定日期之后/后的时间
4.2001年之后/前指定秒数的时间
5.1970年之后/后指定秒数的时间
二、初始化日期
1.init
2.时间间指定秒数的时间
3.指定时间指定秒数之前/后的时间
4.2001年指定秒数之后/前的时间
三、获取时间边界
1.distantFuture
2.distantPast
四、时间比较
1.比较日期是否相等
2.比较日期是否较早
3.比较日期是否较晚
4.带回调的日期比较的方法
五、获取时间间隔
1.与指定日期的时间间隔
2.距离现在的时间间隔
3.距离2001年的时间间隔
4.距离1970年的时间间隔
六、时间的运算
总结 前言 本文整理了一下NSDate的API以便需要或者忘记的时候查询下用法文章末尾贴出了开发过长中常用的NSDate的拓展方法。 系统默认NSDate的默认时间为UTC时间。
一、创建日期
1.获取当前时间 (instancetype)date; 这里返回的当前的时间(UTC时间)下面的示例展示了获取当前时间的方法。
NSDate * todayDate [NSDate date];
NSLog(当前时间:%,todayDate);
2.当前时间指定秒数之后/前的时间 (instancetype)dateWithTimeIntervalSinceNow:(NSTimeInterval)secs; 这里返回的时候当前时间之后多少秒的时间。使用负数表示之前多少秒的时间。
NSDate * todayDate [NSDate date];
NSLog(当前时间:%,todayDate);
NSDate * afterTenSeconds [NSDate dateWithTimeIntervalSinceNow:10];
NSLog(10秒钟之后的时间:%,afterTenSeconds);
NSDate * beforeTenSeconds [NSDate dateWithTimeIntervalSinceNow:-10];
NSLog(10秒钟之前的时间:%,beforeTenSeconds);
3.指定日期之后/后的时间 (instancetype)dateWithTimeInterval:(NSTimeInterval)secsToBeAdded sinceDate:(NSDate *)date; 指定日期多少秒之后的时间负数表示指定日期多少秒之前的时间。
NSDate * todayDate [NSDate date];
NSLog(当前时间:%,todayDate);
NSDate * afterTenSecondDate [NSDate dateWithTimeInterval:10 sinceDate:todayDate];
NSDate * beginTenSecondDate [NSDate dateWithTimeInterval:-10 sinceDate:todayDate];
NSLog(10秒钟之后的时间:%,afterTenSecondDate);
NSLog(10秒钟之后的时间:%,beginTenSecondDate);
4.2001年之后/前指定秒数的时间 (instancetype)dateWithTimeIntervalSinceReferenceDate:(NSTimeInterval)ti; 返回从 2001 年 1 月 1 日 00:00:00 UTC 开始的给定秒数之后的时间负数表示给定秒数之前的时间。 NSDate * todayDate [NSDate date];
NSLog(当前时间:%,todayDate);
NSDate * tenSecondsAfter2001 [NSDate dateWithTimeIntervalSinceReferenceDate:10];
NSLog(2001-1-1 00:00:00 之后10秒的时间当前时间:%,tenSecondsAfter2001);
5.1970年之后/后指定秒数的时间 (instancetype)dateWithTimeIntervalSince1970:(NSTimeInterval)secs; 返回从 1970 年 1 月 1 日 00:00:00 UTC 开始的给定秒数之后的时间负数表示给定秒数之前的时间。
NSDate * tenSecondsAfter1970 [NSDate dateWithTimeIntervalSince1970:10];
NSLog(2001-1-1 00:00:00 之后10秒的时间当前时间:%,tenSecondsAfter1970);
二、初始化日期
1.init - (instancetype)init; 这里返回的也是当前的时间
NSDate * nowDate [[NSDate alloc]init];
NSLog(当前时间:%,nowDate);
2.时间间指定秒数的时间 - (instancetype)initWithTimeIntervalSinceNow:(NSTimeInterval)secs; NSDate * nowDate [[NSDate alloc]init];
NSLog(当前时间:%,nowDate);
NSDate * afterTenSecondsDate [[NSDate alloc]initWithTimeIntervalSinceNow:10];
NSLog(10秒钟之后的时间:%,afterTenSecondsDate);
3.指定时间指定秒数之前/后的时间 - (instancetype)initWithTimeInterval:(NSTimeInterval)secsToBeAdded sinceDate:(NSDate *)date; NSDate * nowDate [[NSDate alloc]init];
NSLog(当前时间:%,nowDate);
NSDate * afterTenSecondsDate [[NSDate alloc]initWithTimeInterval:10 sinceDate:nowDate];
NSLog(10秒钟之后的时间:%,afterTenSecondsDate);
4.2001年指定秒数之后/前的时间 - (instancetype)initWithTimeIntervalSinceReferenceDate:(NSTimeInterval)ti; NSDate * nowDate [[NSDate alloc]init];
NSLog(当前时间:%,nowDate);
NSDate * afterTenSecondsFrom2001 [[NSDate alloc]initWithTimeIntervalSinceReferenceDate:10];
NSLog(2001年10秒钟之后的时间:%,afterTenSecondsFrom2001);
5.1970年指定秒数之后/前的时间 - (instancetype)initWithTimeIntervalSince1970:(NSTimeInterval)secs; NSDate * nowDate [[NSDate alloc]init];
NSLog(当前时间:%,nowDate);
NSDate * afterTenSecondsFrom1970 [[NSDate alloc]initWithTimeIntervalSince1970:10];
NSLog(1970年10秒钟之后的时间:%,afterTenSecondsFrom1970);
三、获取时间边界
1.distantFuture
2.distantPast
四、时间比较
1.比较日期是否相等 - (BOOL)isEqualToDate:(NSDate *)otherDate; 比较A日期是否和B日期是否相同。相同返回YES,不同返回NO。
NSDate * nowDate [NSDate now];
NSDate * nowDateAfter10Seconds [NSDate dateWithTimeIntervalSinceNow:10];
if ([nowDate isEqualToDate:nowDateAfter10Seconds]) {NSLog(nowDate nowDateAfter10Seconds);
} else {NSLog(nowDate ! nowDateAfter10Seconds);
}
if ([[NSDate now] isEqualToDate:[[NSDate alloc]init]]) {NSLog(equal);
}
2.比较日期是否较早 - (NSDate *)earlierDate:(NSDate *)anotherDate; 比较A日期是否早于B日期是的话返回YES,否则返回NO。
NSDate * nowDate [NSDate now];
NSDate * nowDateAfter10Seconds [NSDate dateWithTimeIntervalSinceNow:10];
if ([nowDate isEarlierThanOrEqualTo:nowDateAfter10Seconds]) {NSLog(nowDate isEarlierThanOrEqualTo nowDateAfter10Seconds);
} else {NSLog(nowDate isNotEarlierThanOrEqualTo nowDateAfter10Seconds);
}3.比较日期是否较晚 - (NSDate *)laterDate:(NSDate *)anotherDate; 比较A日期是否晚与B日期和上面的返回结果刚好相反。 NSDate * nowDate [NSDate now];
NSDate * nowDateAfter10Seconds [NSDate dateWithTimeIntervalSinceNow:10];
if ([nowDate isLaterThan:nowDateAfter10Seconds]) {NSLog(nowDate isLaterThan nowDateAfter10Seconds);
} else {NSLog(nowDate isNotLaterThan nowDateAfter10Seconds);
}
4.带回调的日期比较的方法 - (NSComparisonResult)compare:(NSDate *)other; 这里这个方法的返回值是NSComparisonResult类型的枚举当两个日期完全相同的时候返回NSOrderedSame。
A B 返回 NSOrderedAscending A B 返回 NSOrderedSame A B 返回 NSOrderedDescending
NSDate * nowDate [NSDate now];
NSDate * nowDateAfter10Seconds [NSDate dateWithTimeIntervalSinceNow:10];
NSComparisonResult comparisonResult [nowDate compare:nowDateAfter10Seconds];
NSLog(比较结果:%ld,comparisonResult );
五、获取时间间隔
1.与指定日期的时间间隔 - (NSTimeInterval)timeIntervalSinceDate:(NSDate *)anotherDate; 返回和指定日期的时间间隔单位是秒。
NSDate * nowDate [NSDate now];
NSDate * nowDateAfter10Seconds [NSDate dateWithTimeIntervalSinceNow:10];
NSTimeInterval timeInterval [nowDate timeIntervalSinceDate:nowDateAfter10Seconds];
NSLog(nowDate和nowDateAfter10Seconds间隔%lf秒,timeInterval);
2.距离现在的时间间隔 property (readonly) NSTimeInterval timeIntervalSinceNow; 返回指定日期距离现在的时间间隔。
NSDate * nowDate [NSDate now];
NSDate * nowDateAfter10Seconds [NSDate dateWithTimeIntervalSinceNow:10];
NSTimeInterval timeInterval nowDateAfter10Seconds.timeIntervalSinceNow;
NSLog(timeInterval:%lf秒,timeInterval);
3.距离2001年的时间间隔 property (readonly) NSTimeInterval timeIntervalSinceReferenceDate; 返回指定日期距离2001年1月1日的时间间距单位是秒。
NSDate * nowDate [NSDate now];
NSTimeInterval timeInterval nowDate.timeIntervalSinceReferenceDate;
NSLog(timeInterval:%lf秒,timeInterval);
4.距离1970年的时间间隔 property (readonly) NSTimeInterval timeIntervalSince1970; 返回指定日期距离1970年1月1日的时间间距单位是秒。
NSDate * nowDate [NSDate now];
NSTimeInterval timeInterval nowDate.timeIntervalSince1970;
NSLog(timeInterval:%lf秒,timeInterval);
六、时间的运算 - (instancetype)dateByAddingTimeInterval:(NSTimeInterval)ti; 返回指定秒数之后的时间。
NSDate * nowDate [NSDate now];
NSDate * nowDateAfter [nowDate dateByAddingTimeInterval:24 * 60 * 60];
NSLog(nowDateAfter:%,nowDateAfter);
文章转载自: http://www.morning.nylbb.cn.gov.cn.nylbb.cn http://www.morning.dygsz.cn.gov.cn.dygsz.cn http://www.morning.rlkgc.cn.gov.cn.rlkgc.cn http://www.morning.frnjm.cn.gov.cn.frnjm.cn http://www.morning.gtnyq.cn.gov.cn.gtnyq.cn http://www.morning.nxrgl.cn.gov.cn.nxrgl.cn http://www.morning.wwthz.cn.gov.cn.wwthz.cn http://www.morning.xqbbc.cn.gov.cn.xqbbc.cn http://www.morning.zbmcz.cn.gov.cn.zbmcz.cn http://www.morning.stmkm.cn.gov.cn.stmkm.cn http://www.morning.lxfdh.cn.gov.cn.lxfdh.cn http://www.morning.sxygc.cn.gov.cn.sxygc.cn http://www.morning.khzml.cn.gov.cn.khzml.cn http://www.morning.pwggd.cn.gov.cn.pwggd.cn http://www.morning.hfnbr.cn.gov.cn.hfnbr.cn http://www.morning.yzktr.cn.gov.cn.yzktr.cn http://www.morning.hqlnp.cn.gov.cn.hqlnp.cn http://www.morning.sfcfy.cn.gov.cn.sfcfy.cn http://www.morning.zzhqs.cn.gov.cn.zzhqs.cn http://www.morning.hrnrx.cn.gov.cn.hrnrx.cn http://www.morning.xgxbr.cn.gov.cn.xgxbr.cn http://www.morning.rfkyb.cn.gov.cn.rfkyb.cn http://www.morning.kxrhj.cn.gov.cn.kxrhj.cn http://www.morning.tdwjj.cn.gov.cn.tdwjj.cn http://www.morning.kqgsn.cn.gov.cn.kqgsn.cn http://www.morning.srgsb.cn.gov.cn.srgsb.cn http://www.morning.jwdys.cn.gov.cn.jwdys.cn http://www.morning.bpkqd.cn.gov.cn.bpkqd.cn http://www.morning.wfcqr.cn.gov.cn.wfcqr.cn http://www.morning.ytmx.cn.gov.cn.ytmx.cn http://www.morning.yskhj.cn.gov.cn.yskhj.cn http://www.morning.tkrwm.cn.gov.cn.tkrwm.cn http://www.morning.xfyjn.cn.gov.cn.xfyjn.cn http://www.morning.zxxys.cn.gov.cn.zxxys.cn http://www.morning.tgtrk.cn.gov.cn.tgtrk.cn http://www.morning.dzyxr.cn.gov.cn.dzyxr.cn http://www.morning.shuanga.com.cn.gov.cn.shuanga.com.cn http://www.morning.ddfp.cn.gov.cn.ddfp.cn http://www.morning.lflsq.cn.gov.cn.lflsq.cn http://www.morning.dkzwx.cn.gov.cn.dkzwx.cn http://www.morning.gbljq.cn.gov.cn.gbljq.cn http://www.morning.mwrxz.cn.gov.cn.mwrxz.cn http://www.morning.mjqms.cn.gov.cn.mjqms.cn http://www.morning.dbddm.cn.gov.cn.dbddm.cn http://www.morning.wwthz.cn.gov.cn.wwthz.cn http://www.morning.dswtz.cn.gov.cn.dswtz.cn http://www.morning.drrt.cn.gov.cn.drrt.cn http://www.morning.mngyb.cn.gov.cn.mngyb.cn http://www.morning.mtmph.cn.gov.cn.mtmph.cn http://www.morning.eshixi.com.gov.cn.eshixi.com http://www.morning.qpqwd.cn.gov.cn.qpqwd.cn http://www.morning.drjll.cn.gov.cn.drjll.cn http://www.morning.rmlz.cn.gov.cn.rmlz.cn http://www.morning.bxczt.cn.gov.cn.bxczt.cn http://www.morning.rzysq.cn.gov.cn.rzysq.cn http://www.morning.wxccm.cn.gov.cn.wxccm.cn http://www.morning.yrdkl.cn.gov.cn.yrdkl.cn http://www.morning.dpzcc.cn.gov.cn.dpzcc.cn http://www.morning.fnfxp.cn.gov.cn.fnfxp.cn http://www.morning.xqmd.cn.gov.cn.xqmd.cn http://www.morning.trtxt.cn.gov.cn.trtxt.cn http://www.morning.bby45.cn.gov.cn.bby45.cn http://www.morning.yubkwd.cn.gov.cn.yubkwd.cn http://www.morning.dbsch.cn.gov.cn.dbsch.cn http://www.morning.dzqr.cn.gov.cn.dzqr.cn http://www.morning.mlwhd.cn.gov.cn.mlwhd.cn http://www.morning.nspzy.cn.gov.cn.nspzy.cn http://www.morning.mttck.cn.gov.cn.mttck.cn http://www.morning.plchy.cn.gov.cn.plchy.cn http://www.morning.nwjzc.cn.gov.cn.nwjzc.cn http://www.morning.hotlads.com.gov.cn.hotlads.com http://www.morning.xxsrm.cn.gov.cn.xxsrm.cn http://www.morning.xbrxk.cn.gov.cn.xbrxk.cn http://www.morning.gcthj.cn.gov.cn.gcthj.cn http://www.morning.wmfny.cn.gov.cn.wmfny.cn http://www.morning.lrzst.cn.gov.cn.lrzst.cn http://www.morning.bpmmq.cn.gov.cn.bpmmq.cn http://www.morning.fdjwl.cn.gov.cn.fdjwl.cn http://www.morning.nrftd.cn.gov.cn.nrftd.cn http://www.morning.hlshn.cn.gov.cn.hlshn.cn