Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <blockquote> <p>Its a Double Variable.. which will be containing values upto 24 it will be representing HOURS from TODAY..</p> </blockquote> <p>OK, so you have a <em>relative</em> value that you want to add to an <em>absolute</em> value. Additional work is required.</p> <p>First, you must decide "What is today?". Is it 12:01am? If it is, in which time zone? GMT? Something else? You <em>have</em> to know this, because 12:01am GMT is not the same thing as 12:01am EDT. So: what is today?</p> <p>Once you've decided where you're going to be measuring time <em>from</em>, you have to construct an <code>NSDate</code> representing that point in time.</p> <p>For example:</p> <pre><code>NSDate *rightNow = [NSDate date]; NSCalendar *calendar = [NSCalendar currentCalendar]; NSDateComponents *components = [calendar components:NSUIntegerMax fromDate:rightNow]; </code></pre> <p>This will give you an <code>NSDateComponents</code> object, which is an object that represents a point in time <em>relative to a calendar</em>. 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 <code>NSTimeZone</code> object and use <code>-[NSCalendar setTimeZone:]</code> to set the calendar's time zone (before asking for the date components).</p> <p>Now that you've got the date components, we can "reset" things to the appropriate time:</p> <pre><code>[components setHour:0]; [components setMinute:0]; [components setSecond:0]; </code></pre> <p>Then we can turn it back into an <code>NSDate</code> to make it an absolute point in time:</p> <pre><code>NSDate *startingPoint = [calendar dateFromComponents:components]; </code></pre> <p><em>NOW</em> 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:</p> <pre><code>NSInteger hourDelta = var1 / 24; NSDateComponents *delta = [[NSDateComponents alloc] init]; [delta setHour:hourDelta]; </code></pre> <p>Now, we'll add this relative difference to our absolute starting date:</p> <pre><code>NSDate *finalDate = [calendar dateByAddingComponents:delta toDate:startingPoint options:0]; </code></pre> <p>And because we're good citizens, we'll clean up our memory (unless you're using Garbage Collection or compiling with ARC):</p> <pre><code>[delta release]; </code></pre> <p>Some important notes:</p> <ul> <li>All of the manipulations are done via the <code>NSCalendar</code> object, because the <code>NSCalendar</code> 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 <code>NSCalendar</code> define all these rules. Thus, all relative manipulations of dates (adding "hours" and "days" or whatever) must be done via the calendar.</li> <li>There are methods on <code>NSDate</code> like <code>-dateByAddingTimeInterval:</code>, but this is for when you're dealing with absolute intervals and <em>not</em> relative amounts.</li> </ul>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload