开发者

How to Convert a Double value into Date value in Objective-C

开发者 https://www.devze.com 2023-03-26 02:33 出处:网络
I have to convert a Double value into a Date value in Objective-C I tried many ways but im not getting it.

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 int开发者_Go百科o 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?


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号