NSDate Min and Max Possible Values

2020-03-08 08:33发布

问题:

Anyone know what the minimum and maximum possible values are for an NSDate?

回答1:

Use [NSDate distantFuture] and [NSDate distantPast].



回答2:

EDITED Answer:

I thought that I'd discovered the minimum date value was 0001-01-01 00:00:00 + 0000 (given my code below) HOWEVER as has been pointed out in the comments below, this is only for the current era (i.e. A.D.). An NSDate value can go lower than this but what you see as a result of an NSLog may not be what you are expecting, so I would recommend looking at other answers here and ignoring my ignorance.

ORIGINAL Answer:

I believe that I've discovered the minimum date value is 0001-01-01 00:00:00 + 0000

You need to be very careful when NSLog'ing and parsing values to ensure that your timezone isn't skewing the date value slightly.

I will try and demonstrate below:

NSDateFormatter *dateFormatter1 = [[NSDateFormatter alloc] init];
[dateFormatter1 setDateFormat:@"yyyy-MM-dd hh:mm:ss z"];

NSDateFormatter *dateFormatter2 = [[NSDateFormatter alloc] init];
[dateFormatter2 setDateFormat:@"yyyy-MM-dd HH:mm:ss z"];

NSDateFormatter *dateFormatter3 = [[NSDateFormatter alloc] init];
[dateFormatter3 setDateFormat:@"yyyy-MM-dd HH:mm:ss z"];
[dateFormatter3 setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];

NSDate *date1 = [NSDate dateWithTimeIntervalSince1970:-62135596800];
NSLog(@"date1 = %@", date1);
NSLog(@"date1 via dateFormatter1 = %@", [dateFormatter1 stringFromDate:date1]);
NSLog(@"date1 via dateFormatter2 = %@", [dateFormatter2 stringFromDate:date1]);
NSLog(@"date1 via dateFormatter3 = %@", [dateFormatter3 stringFromDate:date1]);

NSDate *date2 = [NSDate dateWithTimeIntervalSince1970:-62135596800 - 100]; // The - 100 makes no difference
NSLog(@"date2 = %@", date2);
NSLog(@"date2 via dateFormatter1 = %@", [dateFormatter1 stringFromDate:date2]);
NSLog(@"date2 via dateFormatter2 = %@", [dateFormatter2 stringFromDate:date2]);
NSLog(@"date2 via dateFormatter3 = %@", [dateFormatter3 stringFromDate:date2]);

This results in the following output (on my iPad given its particular timezone):

date1 = 0001-01-01 00:00:00 +0000
date1 via dateFormatter1 = 0001-12-31 11:58:45 GMT-00:01:15
date1 via dateFormatter2 = 0001-12-31 23:58:45 GMT-00:01:15
date1 via dateFormatter3 = 0001-01-01 00:00:00 GMT
date2 = 0001-12-31 23:58:20 +0000
date2 via dateFormatter1 = 0001-12-31 11:57:05 GMT-00:01:15
date2 via dateFormatter2 = 0001-12-31 23:57:05 GMT-00:01:15
date2 via dateFormatter3 = 0001-12-31 23:58:20 GMT

Note date2 and how subtracting an additional 100 seconds did not reduce the value of the date but actually seemed to increase it. Obviously something peculiar about the way the internals of NSDate work, the time is adjusted by 100 seconds but this skews the day and month perhaps because it couldn't go any lower?



回答3:

maximum should be: 5828963-12-20 00:00:00 +0000 because

NSDate *dateSinceNow = [NSDate dateWithTimeIntervalSinceNow:DBL_MAX];
NSDate *dateSindeReferenceDate = [NSDate dateWithTimeIntervalSinceReferenceDate:DBL_MAX];
NSLog(@"min: %@, max: %@", dateSinceNow, dateSindeReferenceDate);

is the same.

minimum i get this is: 0001-01-01 00:00:00 +0000

cause this is what i get when i try:

NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setDateFormat:@"yyyy-MM-dd hh:mm:ss"];
[df setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];
NSDate *myDate = [df dateFromString: @"0001-01-01 00:00:00"];
NSLog(@"%@", myDate);

but this is current era as pointed out by davedelong

so if you try the first example with - DBL_MAX you get: 41221-07--2147483641 00:00:00 +0000. no idea what era this is...