如何计算Objective-C中两个日期之间的差异?

时间:2021-08-15 21:31:57

I'm working through a challenge in Objective-C Programming, The Big Nerd Ranch Guide, and I'm a little flummoxed by one of the challenges.

我正在通过Objective-C编程,大书呆子牧场指南中的挑战,我对其中一个挑战感到有点沮丧。

Use two instances of NSDate to figure out how many seconds you have been alive. Hint: here is how you create a new date object from the year, month, etc.:

使用两个NSDate实例来计算出你活着的秒数。提示:以下是如何从年,月等创建新的日期对象:

So I need to the difference between now and my date of birth in seconds. Sounds good. Then the hint shows up:

所以我需要现在和我的出生日期之间的差异在几秒钟内。听起来不错。然后提示显示:

@autoreleasepool {
    NSDateComponents *comps = [[NSDateComponents alloc] init];
    [comps setYear:1981];
    [comps setMonth:7];
    [comps setDay:12];
    [comps setHour:1];
    [comps setMinute:55];
    [comps setSecond:33];

    NSCalendar *g = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];

    NSDate *dateOfBirth = [g dateFromComponents:comps];

The first issue is I don't understand what *g is meant to be. I know it's a pointer to an NSCalendar object. But why do we need it? And what is g meant to stand for?

第一个问题是我不明白* g是什么意思。我知道它是一个指向NSCalendar对象的指针。但为什么我们需要呢?什么是g意味着什么?

The sample code then uses the g variable to grab a date. In another language, this would be as easy as DateDiff(dateOne, dateTwo, interval). I'm not clear on why the Calendar object is necessary in the first place, and why we have to create date components to feed the object.

然后,示例代码使用g变量来获取日期。在另一种语言中,这就像DateDiff(dateOne,dateTwo,interval)一样简单。我不清楚为什么首先需要Calendar对象,以及为什么我们必须创建日期组件来提供对象。

This is all new to me, and I've worked with dynamic languages in the past. So a "dummies" like explanation would be great!

这对我来说是全新的,过去我曾使用过动态语言。因此像解释的“假人”会很棒!

2 个解决方案

#1


3  

The sample code then uses the g variable to grab a date. In another language, this would be as easy as DateDiff(dateOne, dateTwo, interval). I'm not clear on why the Calendar object is necessary in the first place, and why we have to create date components to feed the object.

然后,示例代码使用g变量来获取日期。在另一种语言中,这就像DateDiff(dateOne,dateTwo,interval)一样简单。我不清楚为什么首先需要Calendar对象,以及为什么我们必须创建日期组件来提供对象。

I'm not an Objective-C programmer, but I know a bit about date/time APIs.

我不是Objective-C程序员,但我对日期/时间API有所了解。

In order to specify your birth date/time, you're giving a year of 1981, a month of 7 etc. What does a year of 1981 mean? To you, it may mean about 31 years ago... but to someone using a different calendar system, it could mean something entirely different. Converting from "year/month/day etc" to "point in time" is a bit like converting from a string to an integer: does "10" mean ten, or sixteen? It all depends on your frame of reference (the base, in this case - the calendar system in the date case).

为了确定你的出生日期/时间,你给出一年1981年,一个月7等等。1981年的一年是什么意思?对你而言,这可能意味着大约31年前...但对于使用不同日历系统的人来说,它可能意味着完全不同的东西。从“年/月/日等”转换为“时间点”有点像从字符串转换为整数:“10”是指十,还是十六?这一切都取决于您的参考框架(在这种情况下,基数,日期情况下的日历系统)。

The calendar - initialized as a Gregorian calendar - takes those date components and is able to give you back an NSDate which is a sort of "absolute" value in time.

日历 - 初始化为公历 - 获取那些日期组件,并能够返回NSDate,这是一种“绝对”时间值。

As for computing the difference between two NSDate values - I suspect there's a member which gives you something like "seconds since the Unix epoch" (or possible milliseconds). So take that from both dates (your birth and "now"), subtract one from the other, and you'll get the elapsed number of seconds (or milliseconds) between the two.

至于计算两个NSDate值之间的差异 - 我怀疑有一个成员给你类似“自Unix时代以来的秒数”(或可能的毫秒)。所以从两个日期(你的出生和“现在”)中取出它,从另一个中减去一个,你将获得两者之间经过的秒数(或毫秒)。

EDIT: In fact, the timeIntervalSinceNow function is probably the one you want, once you've got your birth date.

编辑:事实上,一旦你有了出生日期,timeIntervalSinceNow函数可能是你想要的。

#2


3  

You can do it this way

你可以这样做

NSDateComponents *comps = [[NSDateComponents alloc] init];
[comps setYear:1981];
[comps setMonth:7];
[comps setDay:12];
[comps setHour:1];
[comps setMinute:55];
[comps setSecond:33];

NSDate *dateOfBirth = [[NSCalendar currentCalendar] dateFromComponents:comps];


NSTimeInterval timeGap=[[NSDate new] timeIntervalSinceDate:dateOfBirth ];

#1


3  

The sample code then uses the g variable to grab a date. In another language, this would be as easy as DateDiff(dateOne, dateTwo, interval). I'm not clear on why the Calendar object is necessary in the first place, and why we have to create date components to feed the object.

然后,示例代码使用g变量来获取日期。在另一种语言中,这就像DateDiff(dateOne,dateTwo,interval)一样简单。我不清楚为什么首先需要Calendar对象,以及为什么我们必须创建日期组件来提供对象。

I'm not an Objective-C programmer, but I know a bit about date/time APIs.

我不是Objective-C程序员,但我对日期/时间API有所了解。

In order to specify your birth date/time, you're giving a year of 1981, a month of 7 etc. What does a year of 1981 mean? To you, it may mean about 31 years ago... but to someone using a different calendar system, it could mean something entirely different. Converting from "year/month/day etc" to "point in time" is a bit like converting from a string to an integer: does "10" mean ten, or sixteen? It all depends on your frame of reference (the base, in this case - the calendar system in the date case).

为了确定你的出生日期/时间,你给出一年1981年,一个月7等等。1981年的一年是什么意思?对你而言,这可能意味着大约31年前...但对于使用不同日历系统的人来说,它可能意味着完全不同的东西。从“年/月/日等”转换为“时间点”有点像从字符串转换为整数:“10”是指十,还是十六?这一切都取决于您的参考框架(在这种情况下,基数,日期情况下的日历系统)。

The calendar - initialized as a Gregorian calendar - takes those date components and is able to give you back an NSDate which is a sort of "absolute" value in time.

日历 - 初始化为公历 - 获取那些日期组件,并能够返回NSDate,这是一种“绝对”时间值。

As for computing the difference between two NSDate values - I suspect there's a member which gives you something like "seconds since the Unix epoch" (or possible milliseconds). So take that from both dates (your birth and "now"), subtract one from the other, and you'll get the elapsed number of seconds (or milliseconds) between the two.

至于计算两个NSDate值之间的差异 - 我怀疑有一个成员给你类似“自Unix时代以来的秒数”(或可能的毫秒)。所以从两个日期(你的出生和“现在”)中取出它,从另一个中减去一个,你将获得两者之间经过的秒数(或毫秒)。

EDIT: In fact, the timeIntervalSinceNow function is probably the one you want, once you've got your birth date.

编辑:事实上,一旦你有了出生日期,timeIntervalSinceNow函数可能是你想要的。

#2


3  

You can do it this way

你可以这样做

NSDateComponents *comps = [[NSDateComponents alloc] init];
[comps setYear:1981];
[comps setMonth:7];
[comps setDay:12];
[comps setHour:1];
[comps setMinute:55];
[comps setSecond:33];

NSDate *dateOfBirth = [[NSCalendar currentCalendar] dateFromComponents:comps];


NSTimeInterval timeGap=[[NSDate new] timeIntervalSinceDate:dateOfBirth ];