ios中NSDate dateWithTimeIntervalSinceNow:方法的限制

只能通过NSDate dateWithTimeIntervalSinceNow:方法获取前后68年的日期。

测试代码:

int now = [NSDate timeIntervalSinceReferenceDate];

NSDateFormatter *formatter = [NSDateFormatter new];

formatter.dateFormat = @"yyyy-MM-dd";

NSLog(@"now:%@", [formatter stringFromDate:[NSDate dateWithTimeIntervalSinceNow:0]]);

for (int i=10;i<500;i+=10) {

NSLog(@"%d year ago:%@",i,[formatter stringFromDate:[NSDate dateWithTimeIntervalSinceNow:-i*365*24*60*60]]);

}

2014-07-27 15:40:59.511 com_dreaminto_libs[5425:60b] now:2014-07-27

2014-07-27 15:37:28.119 com_dreaminto_libs[5418:60b] 10 year ago:2004-07-29

2014-07-27 15:37:28.121 com_dreaminto_libs[5418:60b] 20 year ago:1994-08-01

2014-07-27 15:37:28.122 com_dreaminto_libs[5418:60b] 30 year ago:1984-08-03

2014-07-27 15:37:28.124 com_dreaminto_libs[5418:60b] 40 year ago:1974-08-06

2014-07-27 15:37:28.125 com_dreaminto_libs[5418:60b] 50 year ago:1964-08-08

2014-07-27 15:37:28.127 com_dreaminto_libs[5418:60b] 60 year ago:1954-08-11

步长缩短至1,代码变为

for (int i=60;i<100;i++) {

NSLog(@"%d year ago:%@",i,[formatter stringFromDate:[NSDate dateWithTimeIntervalSinceNow:-i*365*24*60*60]]);

}

2014-07-27 15:40:59.513 com_dreaminto_libs[5425:60b] 60 year ago:1954-08-11

2014-07-27 15:40:59.514 com_dreaminto_libs[5425:60b] 61 year ago:1953-08-11

2014-07-27 15:40:59.516 com_dreaminto_libs[5425:60b] 62 year ago:1952-08-11

2014-07-27 15:40:59.518 com_dreaminto_libs[5425:60b] 63 year ago:1951-08-12

2014-07-27 15:40:59.519 com_dreaminto_libs[5425:60b] 64 year ago:1950-08-12

2014-07-27 15:40:59.521 com_dreaminto_libs[5425:60b] 65 year ago:1949-08-12

2014-07-27 15:40:59.523 com_dreaminto_libs[5425:60b] 66 year ago:1948-08-12

2014-07-27 15:40:59.524 com_dreaminto_libs[5425:60b] 67 year ago:1947-08-13

2014-07-27 15:40:59.526 com_dreaminto_libs[5425:60b] 68 year ago:1946-08-13

2014-07-27 15:40:59.528 com_dreaminto_libs[5425:60b] 69
year ago:2081-09-18

2014-07-27 15:40:59.529 com_dreaminto_libs[5425:60b] 70 year ago:2080-09-18

2014-07-27 15:40:59.531 com_dreaminto_libs[5425:60b] 71 year ago:2079-09-19

----------------

for (int i=1;i<100;i++) {

NSLog(@"after %d year :%@",i,[formatter stringFromDate:[NSDate dateWithTimeIntervalSinceNow:i*365*24*60*60]]);

}

2014-07-27 15:42:52.228 com_dreaminto_libs[5432:60b] after 28 year :2042-07-20

2014-07-27 15:42:52.230 com_dreaminto_libs[5432:60b] after 29 year :2043-07-20

2014-07-27 15:42:52.231 com_dreaminto_libs[5432:60b] after 30 year :2044-07-19

2014-07-27 15:42:52.233 com_dreaminto_libs[5432:60b] after 31 year :2045-07-19

2014-07-27 15:42:52.235 com_dreaminto_libs[5432:60b] after 32 year :2046-07-19

2014-07-27 15:42:52.236 com_dreaminto_libs[5432:60b] after 33 year :2047-07-19

2014-07-27 15:42:52.238 com_dreaminto_libs[5432:60b] after 34 year :2048-07-18

2014-07-27 15:42:52.240 com_dreaminto_libs[5432:60b] after 35 year :2049-07-18

2014-07-27 15:42:52.242 com_dreaminto_libs[5432:60b] after 36 year :2050-07-18

2014-07-27 15:42:52.244 com_dreaminto_libs[5432:60b] after 37 year :2051-07-18

2014-07-27 15:42:52.245 com_dreaminto_libs[5432:60b] after 38 year :2052-07-17

2014-07-27 15:42:52.247 com_dreaminto_libs[5432:60b] after 39 year :2053-07-17

2014-07-27 15:42:52.248 com_dreaminto_libs[5432:60b] after 40 year :2054-07-17

2014-07-27 15:42:52.250 com_dreaminto_libs[5432:60b] after 41 year :2055-07-17

2014-07-27 15:42:52.252 com_dreaminto_libs[5432:60b] after 42 year :2056-07-16

2014-07-27 15:42:52.253 com_dreaminto_libs[5432:60b] after 43 year :2057-07-16

2014-07-27 15:42:52.255 com_dreaminto_libs[5432:60b] after 44 year :2058-07-16

2014-07-27 15:42:52.256 com_dreaminto_libs[5432:60b] after 45 year :2059-07-16

2014-07-27 15:42:52.258 com_dreaminto_libs[5432:60b] after 46 year :2060-07-15

2014-07-27 15:42:52.260 com_dreaminto_libs[5432:60b] after 47 year :2061-07-15

2014-07-27 15:42:52.261 com_dreaminto_libs[5432:60b] after 48 year :2062-07-15

2014-07-27 15:42:52.263 com_dreaminto_libs[5432:60b] after 49 year :2063-07-15

2014-07-27 15:42:52.265 com_dreaminto_libs[5432:60b] after 50 year :2064-07-14

2014-07-27 15:42:52.266 com_dreaminto_libs[5432:60b] after 51 year :2065-07-14

2014-07-27 15:42:52.268 com_dreaminto_libs[5432:60b] after 52 year :2066-07-14

2014-07-27 15:42:52.270 com_dreaminto_libs[5432:60b] after 53 year :2067-07-14

2014-07-27 15:42:52.271 com_dreaminto_libs[5432:60b] after 54 year :2068-07-13

2014-07-27 15:42:52.273 com_dreaminto_libs[5432:60b] after 55 year :2069-07-13

2014-07-27 15:42:52.275 com_dreaminto_libs[5432:60b] after 56 year :2070-07-13

2014-07-27 15:42:52.276 com_dreaminto_libs[5432:60b] after 57 year :2071-07-13

2014-07-27 15:42:52.278 com_dreaminto_libs[5432:60b] after 58 year :2072-07-12

2014-07-27 15:42:52.280 com_dreaminto_libs[5432:60b] after 59 year :2073-07-12

2014-07-27 15:42:52.281 com_dreaminto_libs[5432:60b] after 60 year :2074-07-12

2014-07-27 15:42:52.283 com_dreaminto_libs[5432:60b] after 61 year :2075-07-12

2014-07-27 15:42:52.284 com_dreaminto_libs[5432:60b] after 62 year :2076-07-11

2014-07-27 15:42:52.286 com_dreaminto_libs[5432:60b] after 63 year :2077-07-11

2014-07-27 15:42:52.288 com_dreaminto_libs[5432:60b] after 64 year :2078-07-11

2014-07-27 15:42:52.290 com_dreaminto_libs[5432:60b] after 65 year :2079-07-11

2014-07-27 15:42:52.291 com_dreaminto_libs[5432:60b] after 66 year :2080-07-10

2014-07-27 15:42:52.293 com_dreaminto_libs[5432:60b] after 67 year :2081-07-10

2014-07-27 15:42:52.294 com_dreaminto_libs[5432:60b] after 68 year :2082-07-10

2014-07-27 15:42:52.296 com_dreaminto_libs[5432:60b] after 69
year :1947-06-04

2014-07-27 15:42:52.298 com_dreaminto_libs[5432:60b] after 70 year :1948-06-03

2014-07-27 15:42:52.299 com_dreaminto_libs[5432:60b] after 71 year :1949-06-03

结论:只能通过NSDate dateWithTimeIntervalSinceNow:方法获取前后68年的日期。

但不确定是否与NSDateFormatter 有关系。

测试代码:

int now = [NSDate timeIntervalSinceReferenceDate];

NSDateFormatter *formatter = [NSDateFormatter new];

formatter.dateFormat = @"yyyy-MM-dd";

NSLog(@"now:%@", [formatter stringFromDate:[NSDate dateWithTimeIntervalSinceNow:0]]);

for (int i=10;i<500;i+=10) {

NSLog(@"%d year ago:%@",i,[formatter stringFromDate:[NSDate dateWithTimeIntervalSinceNow:-i*365*24*60*60]]);

}

2014-07-27 15:40:59.511 com_dreaminto_libs[5425:60b] now:2014-07-27

2014-07-27 15:37:28.119 com_dreaminto_libs[5418:60b] 10 year ago:2004-07-29

2014-07-27 15:37:28.121 com_dreaminto_libs[5418:60b] 20 year ago:1994-08-01

2014-07-27 15:37:28.122 com_dreaminto_libs[5418:60b] 30 year ago:1984-08-03

2014-07-27 15:37:28.124 com_dreaminto_libs[5418:60b] 40 year ago:1974-08-06

2014-07-27 15:37:28.125 com_dreaminto_libs[5418:60b] 50 year ago:1964-08-08

2014-07-27 15:37:28.127 com_dreaminto_libs[5418:60b] 60 year ago:1954-08-11

步长缩短至1,代码变为

for (int i=60;i<100;i++) {

NSLog(@"%d year ago:%@",i,[formatter stringFromDate:[NSDate dateWithTimeIntervalSinceNow:-i*365*24*60*60]]);

}

2014-07-27 15:40:59.513 com_dreaminto_libs[5425:60b] 60 year ago:1954-08-11

2014-07-27 15:40:59.514 com_dreaminto_libs[5425:60b] 61 year ago:1953-08-11

2014-07-27 15:40:59.516 com_dreaminto_libs[5425:60b] 62 year ago:1952-08-11

2014-07-27 15:40:59.518 com_dreaminto_libs[5425:60b] 63 year ago:1951-08-12

2014-07-27 15:40:59.519 com_dreaminto_libs[5425:60b] 64 year ago:1950-08-12

2014-07-27 15:40:59.521 com_dreaminto_libs[5425:60b] 65 year ago:1949-08-12

2014-07-27 15:40:59.523 com_dreaminto_libs[5425:60b] 66 year ago:1948-08-12

2014-07-27 15:40:59.524 com_dreaminto_libs[5425:60b] 67 year ago:1947-08-13

2014-07-27 15:40:59.526 com_dreaminto_libs[5425:60b] 68 year ago:1946-08-13

2014-07-27 15:40:59.528 com_dreaminto_libs[5425:60b] 69
year ago:2081-09-18

2014-07-27 15:40:59.529 com_dreaminto_libs[5425:60b] 70 year ago:2080-09-18

2014-07-27 15:40:59.531 com_dreaminto_libs[5425:60b] 71 year ago:2079-09-19

----------------

for (int i=1;i<100;i++) {

NSLog(@"after %d year :%@",i,[formatter stringFromDate:[NSDate dateWithTimeIntervalSinceNow:i*365*24*60*60]]);

}

2014-07-27 15:42:52.228 com_dreaminto_libs[5432:60b] after 28 year :2042-07-20

2014-07-27 15:42:52.230 com_dreaminto_libs[5432:60b] after 29 year :2043-07-20

2014-07-27 15:42:52.231 com_dreaminto_libs[5432:60b] after 30 year :2044-07-19

2014-07-27 15:42:52.233 com_dreaminto_libs[5432:60b] after 31 year :2045-07-19

2014-07-27 15:42:52.235 com_dreaminto_libs[5432:60b] after 32 year :2046-07-19

2014-07-27 15:42:52.236 com_dreaminto_libs[5432:60b] after 33 year :2047-07-19

2014-07-27 15:42:52.238 com_dreaminto_libs[5432:60b] after 34 year :2048-07-18

2014-07-27 15:42:52.240 com_dreaminto_libs[5432:60b] after 35 year :2049-07-18

2014-07-27 15:42:52.242 com_dreaminto_libs[5432:60b] after 36 year :2050-07-18

2014-07-27 15:42:52.244 com_dreaminto_libs[5432:60b] after 37 year :2051-07-18

2014-07-27 15:42:52.245 com_dreaminto_libs[5432:60b] after 38 year :2052-07-17

2014-07-27 15:42:52.247 com_dreaminto_libs[5432:60b] after 39 year :2053-07-17

2014-07-27 15:42:52.248 com_dreaminto_libs[5432:60b] after 40 year :2054-07-17

2014-07-27 15:42:52.250 com_dreaminto_libs[5432:60b] after 41 year :2055-07-17

2014-07-27 15:42:52.252 com_dreaminto_libs[5432:60b] after 42 year :2056-07-16

2014-07-27 15:42:52.253 com_dreaminto_libs[5432:60b] after 43 year :2057-07-16

2014-07-27 15:42:52.255 com_dreaminto_libs[5432:60b] after 44 year :2058-07-16

2014-07-27 15:42:52.256 com_dreaminto_libs[5432:60b] after 45 year :2059-07-16

2014-07-27 15:42:52.258 com_dreaminto_libs[5432:60b] after 46 year :2060-07-15

2014-07-27 15:42:52.260 com_dreaminto_libs[5432:60b] after 47 year :2061-07-15

2014-07-27 15:42:52.261 com_dreaminto_libs[5432:60b] after 48 year :2062-07-15

2014-07-27 15:42:52.263 com_dreaminto_libs[5432:60b] after 49 year :2063-07-15

2014-07-27 15:42:52.265 com_dreaminto_libs[5432:60b] after 50 year :2064-07-14

2014-07-27 15:42:52.266 com_dreaminto_libs[5432:60b] after 51 year :2065-07-14

2014-07-27 15:42:52.268 com_dreaminto_libs[5432:60b] after 52 year :2066-07-14

2014-07-27 15:42:52.270 com_dreaminto_libs[5432:60b] after 53 year :2067-07-14

2014-07-27 15:42:52.271 com_dreaminto_libs[5432:60b] after 54 year :2068-07-13

2014-07-27 15:42:52.273 com_dreaminto_libs[5432:60b] after 55 year :2069-07-13

2014-07-27 15:42:52.275 com_dreaminto_libs[5432:60b] after 56 year :2070-07-13

2014-07-27 15:42:52.276 com_dreaminto_libs[5432:60b] after 57 year :2071-07-13

2014-07-27 15:42:52.278 com_dreaminto_libs[5432:60b] after 58 year :2072-07-12

2014-07-27 15:42:52.280 com_dreaminto_libs[5432:60b] after 59 year :2073-07-12

2014-07-27 15:42:52.281 com_dreaminto_libs[5432:60b] after 60 year :2074-07-12

2014-07-27 15:42:52.283 com_dreaminto_libs[5432:60b] after 61 year :2075-07-12

2014-07-27 15:42:52.284 com_dreaminto_libs[5432:60b] after 62 year :2076-07-11

2014-07-27 15:42:52.286 com_dreaminto_libs[5432:60b] after 63 year :2077-07-11

2014-07-27 15:42:52.288 com_dreaminto_libs[5432:60b] after 64 year :2078-07-11

2014-07-27 15:42:52.290 com_dreaminto_libs[5432:60b] after 65 year :2079-07-11

2014-07-27 15:42:52.291 com_dreaminto_libs[5432:60b] after 66 year :2080-07-10

2014-07-27 15:42:52.293 com_dreaminto_libs[5432:60b] after 67 year :2081-07-10

2014-07-27 15:42:52.294 com_dreaminto_libs[5432:60b] after 68 year :2082-07-10

2014-07-27 15:42:52.296 com_dreaminto_libs[5432:60b] after 69
year :1947-06-04

2014-07-27 15:42:52.298 com_dreaminto_libs[5432:60b] after 70 year :1948-06-03

2014-07-27 15:42:52.299 com_dreaminto_libs[5432:60b] after 71 year :1949-06-03

结论:只能通过NSDate dateWithTimeIntervalSinceNow:方法获取前后68年的日期。

但不确定是否与NSDateFormatter 有关系。

时间: 2024-10-20 06:03:24

ios中NSDate dateWithTimeIntervalSinceNow:方法的限制的相关文章

IOS中NSdate用法

// date方法返回的就是当前时间(now) 02. NSDate *date = [NSDate date]; 03.// now: 11:12:40 04.// date: 11:12:50 05. date = [NSDate dateWithTimeIntervalSinceNow:10];//返回当前时间10秒后的时间 06. // 从1970-1-1 00:00:00开始 07. date = [NSDate dateWithTimeIntervalSince1970:10];//

ios中集合遍历方法的比较和技巧

本文原文发表自我的[自建博客],cnblogs同步发表,格式未经调整,内容以原博客为准 我是前言 集合的遍历操作是开发中最常见的操作之一,从C语言经典的for循环到利用多核cpu的优势进行遍历,开发中ios有若干集合遍历方法,本文通过研究和测试比较了各个操作方法的效率和优略势,并总结几个使用集合遍历时的小技巧. ios中常用的遍历运算方法 遍历的目的是获取集合中的某个对象或执行某个操作,所以能满足这个条件的方法都可以作为备选: 经典for循环 for in (NSFastEnumeration)

分享iOS中的sizeToFit()方法的使用心得

第一次写技术博客,之前没有写,主要因为认为自己对iOS技术还没有研究的那么深,怕写出来误人子弟.第二个,也是自己比较懒吧.为了督促自己,也是为了和更多的iOS开发爱好者一起交流和共同进步,绝对以后有什么心得,都会分享.(毕竟稍后等于永不)同时也希望大家积极指出我的错误和不完整的地方. 好,接下我们进入正题: sizeToFit()使用心得: 很多的初学者,包括我在内,当初在学习的时候,特别纠结什么时候用这个sizeToFit(). 下面我就来分享一下我的一些使用心得. 一.我们先来看看官方文档对

iOS 中可变参数方法的声明和实现

// NS_REQUIRES_NIL_TERMINATION, 用于编译时非nil结尾的检查 - (void)mutableArgumentsFunction:(NSString *)arg1, ... NS_REQUIRES_NIL_TERMINATION { // 定义一个指向可选参数列表的指针 va_list args; // 获取第一个可选参数的地址,此时参数列表指针指向函数参数列表中的第一个可选参数 va_start(args, arg1); if(arg1) { // 遍历参数列表中

4. iOS中常用演示方法以及利弊

- (void)delay1 { // 延迟执行不要用sleep,坏处:卡住当前线程 [NSThread sleepForTimeInterval:3]; NSLog(@"-----下载图片-----"); } - (void)delay2 { // 一旦定制好延迟任务后,不会卡主当前线程(延时任务在哪条线程执行取决于当前代码在何处调用) [self performSelector:@selector(download:) withObject:@"http://555.jp

IOS中UIActionSheet使用方法详解

一.初始化方法 - (instancetype)initWithTitle:(NSString *)title delegate:(id<UIActionSheetDelegate>)delegate cancelButtonTitle:(NSString *)cancelButtonTitle destructiveButtonTitle:(NSString *)destructiveButtonTitle otherButtonTitles:(NSString *)otherButtonT

iOS 中自定义TableViewCell方法

自定义cell时需要继承UITableViewCell. 举例:ZLSchoolListTableCell继承UITableViewCell ZLSchoolListTableCell.h文件 #import <UIKit/UIKit.h> @class SchoolModel(模型); @interface ZLSchoolListTableCell : UITableViewCell +(instancetype)SchoolListWithTableView:(UITableView*)

IOS中UIActionSheet使用详解

IOS中UIActionSheet使用方法详解 一.初始化方法 - (instancetype)initWithTitle:(NSString *)title delegate:(id<UIActionSheetDelegate>)delegate cancelButtonTitle:(NSString *)cancelButtonTitle destructiveButtonTitle:(NSString *)destructiveButtonTitle otherButtonTitles:

ios中利用NSDateComponents、NSDate、NSCalendar判断当前时间是否在一天的某个时间段内。

应用中设置一般会存在这样的设置,如夜间勿扰模式,从8:00-23:00,此时如何判断当前时间是否在该时间段内.难点主要在于如何用NSDate生成一个8:00的时间和23:00的时间,然后用当前的时间跟这俩时间作对比就好了. 下面提供两条思路: 法1.用NSDate生成当前时间,然后转为字符串,从字符串中取出当前的年.月.日,然后再拼上时.分.秒,然后再将拼接后的字符串转为NSDate,最后用当前的时间跟自己生成的俩NSDate的时间点比较.(该方法比较笨,也不难,但看起来有点太菜了,看上去不怎么