NSDate Min和最大值可能值。

时间:2022-02-28 07:48:23

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

有人知道NSDate的最小和最大值是多少吗?

3 个解决方案

#1


41  

Use [NSDate distantFuture] and [NSDate distantPast].

使用[NSDate distantFuture]和[NSDate distantPast]。

#2


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.

我认为我发现了最小的日期值是0001-01-01 00:00 + 0000(根据我下面的代码)但是正如下面的评论所指出的,这只是针对当前的时代(即公元)。NSDate值可以低于这个值但是NSLog的结果可能不是你所期望的,所以我建议你看看这里的其他答案,忽略我的无知。

ORIGINAL Answer:

最初的回答:

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

我相信我已经发现最小的日期值是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.

在使用NSLog和解析值时,您需要非常小心,以确保您的时区不会稍微偏离日期值。

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):

这将产生以下输出(在我的iPad上,给定其特定的时区):

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?

注意date2和减去额外的100秒并没有降低日期的价值,但实际上似乎增加了日期。显然NSDate的内部工作方式有些特别,时间被调整了100秒但这扭曲了一天又一月也许是因为它不能再低了?

#3


1  

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

最大值应该是:5828963-12 00:00 +0000,因为

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

我得到的最小值是:0001-01-01 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

但正如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...

如果你用- DBL_MAX尝试第一个例子,你会得到:41221-07——2147483641 00:00 00:00 +0000。不知道这是什么时代…

#1


41  

Use [NSDate distantFuture] and [NSDate distantPast].

使用[NSDate distantFuture]和[NSDate distantPast]。

#2


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.

我认为我发现了最小的日期值是0001-01-01 00:00 + 0000(根据我下面的代码)但是正如下面的评论所指出的,这只是针对当前的时代(即公元)。NSDate值可以低于这个值但是NSLog的结果可能不是你所期望的,所以我建议你看看这里的其他答案,忽略我的无知。

ORIGINAL Answer:

最初的回答:

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

我相信我已经发现最小的日期值是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.

在使用NSLog和解析值时,您需要非常小心,以确保您的时区不会稍微偏离日期值。

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):

这将产生以下输出(在我的iPad上,给定其特定的时区):

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?

注意date2和减去额外的100秒并没有降低日期的价值,但实际上似乎增加了日期。显然NSDate的内部工作方式有些特别,时间被调整了100秒但这扭曲了一天又一月也许是因为它不能再低了?

#3


1  

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

最大值应该是:5828963-12 00:00 +0000,因为

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

我得到的最小值是:0001-01-01 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

但正如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...

如果你用- DBL_MAX尝试第一个例子,你会得到:41221-07——2147483641 00:00 00:00 +0000。不知道这是什么时代…