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 有关系。

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。