如何在 Objective-C 中将 Double 值转换为 Date 值

发布于 2024-11-27 20:09:52 字数 313 浏览 2 评论 0原文

我必须在 Objective-C 中将 Double 值转换为 Date 值 我尝试了很多方法,但我不明白。

date=var1/24;

这里 var1 是一个双精度值,应除以 24 并作为日期存储到变量 date 中。我如何使用 Objective-C 来做到这一点?

我创建了这样的日期变量:

NSDate *date=[[NSDate alloc]init];
date=(nsdate)var1/24;

我怎样才能做到这一点?

I have to convert a Double value into a Date value in Objective-C
I tried many ways but im not getting it.

date=var1/24;

Here var1 is a double value which should be divided by 24 and stored as a Date into the variable date. How can I do this using Objective-C?

I created the date variable like this:

NSDate *date=[[NSDate alloc]init];
date=(nsdate)var1/24;

How can I do this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

风铃鹿 2024-12-04 20:09:52

它是一个双精度变量..它将包含最多 24 的值,它将代表从今天开始的小时数..

好的,所以您有一个想要添加到绝对值相对值 em>值。需要额外的工作。

首先,您必须决定“今天是什么日子?”。现在是中午 12:01 吗?如果是的话,在哪个时区?格林威治标准时间?还有别的事吗?您必须知道这一点,因为 GMT 上午 12:01 与 EDT 上午 12:01 不同。那么:今天是什么日子?

一旦您决定要从哪里测量时间,您就必须构造一个表示该时间点的 NSDate

例如:

NSDate *rightNow = [NSDate date];
NSCalendar *calendar = [NSCalendar currentCalendar];
NSDateComponents *components = [calendar components:NSUIntegerMax fromDate:rightNow];

这将为您提供一个 NSDateComponents 对象,该对象表示相对于日历的时间点。在本例中,我们使用“当前日历”(可能是公历,但不一定),默认时区是您当前的时区。如果您需要让它相对于不同的时区,您可以创建一个新的 NSTimeZone 对象并使用 -[NSCalendar setTimeZone:] 设置日历的时区(在询问日期部分之前)。

现在您已经获得了日期组件,我们可以将其“重置”到适当的时间:

[components setHour:0];
[components setMinute:0];
[components setSecond:0];

然后我们可以将其转回 NSDate 以使其成为绝对时间点:

NSDate *startingPoint = [calendar dateFromComponents:components];

NOW 我们有我们的起点,并且可以处理“从今天开始的几个小时”。首先,我们将创建一个日期组件对象来表示差异的小时数:

NSInteger hourDelta = var1 / 24;
NSDateComponents *delta = [[NSDateComponents alloc] init];
[delta setHour:hourDelta];

现在,我们将这个相对差异添加到我们的绝对开始日期中:

NSDate *finalDate = [calendar dateByAddingComponents:delta toDate:startingPoint options:0];

并且因为我们是好公民,所以我们将清理我们的内存(除非您使用垃圾收集或使用 ARC 进行编译):

[delta release];

一些重要注意事项:

  • 所有操作都是通过 NSCalendar 对象完成的,因为 NSCalendar 定义了什么是“日”是什么意思“小时”是。你认为“一天有 24 小时,一小时有 60 分钟……”,但这不一定是真的。我可以创建自己的日历,一天有 10 小时,一小时有 10 分钟。如果我愿意,我可以将“一天”定义为“地球自转一周”以外的其他内容。 NSCalendar 定义了所有这些规则。因此,所有日期的相关操作(添加“小时”和“天”或其他内容)都必须通过日历完成。
  • NSDate 上有一些方法,例如 -dateByAddingTimeInterval:,但这适用于处理绝对间隔而不是相对数量的情况。

Its a Double Variable.. which will be containing values upto 24 it will be representing HOURS from TODAY..

OK, so you have a relative value that you want to add to an absolute value. Additional work is required.

First, you must decide "What is today?". Is it 12:01am? If it is, in which time zone? GMT? Something else? You have to know this, because 12:01am GMT is not the same thing as 12:01am EDT. So: what is today?

Once you've decided where you're going to be measuring time from, you have to construct an NSDate representing that point in time.

For example:

NSDate *rightNow = [NSDate date];
NSCalendar *calendar = [NSCalendar currentCalendar];
NSDateComponents *components = [calendar components:NSUIntegerMax fromDate:rightNow];

This will give you an NSDateComponents object, which is an object that represents a point in time relative to a calendar. In this case, we're using the "current calendar" (probably the Gregorian calendar, but not necessarily), and the default time zone is your current time zone. If you need to have it be relative to a different time zone, you can create a new NSTimeZone object and use -[NSCalendar setTimeZone:] to set the calendar's time zone (before asking for the date components).

Now that you've got the date components, we can "reset" things to the appropriate time:

[components setHour:0];
[components setMinute:0];
[components setSecond:0];

Then we can turn it back into an NSDate to make it an absolute point in time:

NSDate *startingPoint = [calendar dateFromComponents:components];

NOW we have our starting point, and can deal with the "hours from today". First, we'll create a date components object to represent however many hours the difference is:

NSInteger hourDelta = var1 / 24;
NSDateComponents *delta = [[NSDateComponents alloc] init];
[delta setHour:hourDelta];

Now, we'll add this relative difference to our absolute starting date:

NSDate *finalDate = [calendar dateByAddingComponents:delta toDate:startingPoint options:0];

And because we're good citizens, we'll clean up our memory (unless you're using Garbage Collection or compiling with ARC):

[delta release];

Some important notes:

  • All of the manipulations are done via the NSCalendar object, because the NSCalendar is what defines what a "day" means and what an "hour" is. You think "there are 24 hours in a day and 60 minutes in an hour...", but that's not necessarily true. I can create my own calendar that has 10 hours in a day, and 10 minutes in an hour. If I want, I can define a "day" as something other than "one full rotation of the Earth". The NSCalendar define all these rules. Thus, all relative manipulations of dates (adding "hours" and "days" or whatever) must be done via the calendar.
  • There are methods on NSDate like -dateByAddingTimeInterval:, but this is for when you're dealing with absolute intervals and not relative amounts.
小猫一只 2024-12-04 20:09:52

这取决于您想要做什么,但直接转换为 (nsdate)var1/24 肯定工作!

无论如何,你的变量 var1 到底代表什么?分钟 ?秒?小时?从哪个参考日期开始?今天? UNIX 时代?它是 UNIX 时间戳(自 1970 年 1 月 1 日以来的秒数)吗?只是要求“将数字转换为日期”本身没有任何意义;-)

根据这些问题的答案,您可以使用 NSDateComponents 来生成一个日期,给出不同的日期组件(月、日、小时、分钟、秒,...),或者使用 dateWithTimeIntervalSince1970 从 UNIX 时间戳创建 NSDate...或者根据您的需要使用 NSDate 或 NSDateComponents 中的任何其他方法。

无论您遇到什么问题,一定要阅读*日期和时间编程指南!关于日期操作的一切在这里都有解释;它将包含回答您的问题所需的一切。

It depends of what you want to do exactly, but a direct cast as (nsdate)var1/24 will definitely NOT work!!!

Anway, what does your variable var1 represent exactly? Minutes ? Seconds ? Hours ? From which reference date? Today? The UNIX Epoch? Is it a UNIX timestamp (seconds since 01/01/1970)? Just asking to "convert a number to a date" means nothing on its own ;-)

Depending on the answer to those questions, you may use either NSDateComponents to produce a date giving the different date components (month, day, hours, minutes, seconds, …), or create a NSDate from a UNIX timestamp using dateWithTimeIntervalSince1970... or use any other method from NSDate or NSDateComponents depending on your needs.

Whatever your problem is, do read* the Date and TIme Programming Guide!! Everything is explained here about date manipulation; it will contain everything you need to answer your question.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文