iPhone : Exploring the Foundation Framework – NS Date & Timer Class


NSDate objects encapsulate a single point in time, independent of any particular calendrical system or time zone. Date objects are immutable, representing an invariant time interval relative to an absolute reference date (00:00:00 UTC on 1 January 2001).

The NSDate class provides methods for comparing dates, calculating the time interval between two dates, and creating a new date from a time interval relative to another date. NSDate objects can be used in conjunction with NSDateFormatter objects to create localized representations of dates and times, as well as with NSCalendar objects to perform calendar arithmetic. (To know further class types, refer to Exploring the Foundation Framework).

   NSDate *aDate  =  [NSDate   date];
NSLog   (@ “ a Date is %@”, aDate );

// converting date object to a string:

NSDateFormatter * dateFormatter   =
[[NSDateFormatter alloc] init];
[date formatter SetDateStyle: NSDateFormatterShort Style]
[date for matter set time style : ns date for matter no style ]
[date formatter SetDateFormat:@ “mm –dd- yyyy”]
NSString * dateString   = [ date formatter StringFromDate : a date];
NSLog (@  ‘dateString is % @”,  dateString);

// converting string to a date:

NSDate * bDate = [ date formatter dateFromString : date string];
NSLog (@  “bDate is %@”, bDate);

// time interval b/w 2 dates:

NSTimeInterval =  [ aDate timeIntervalSinceDate b date
NSLog (@ “ the timeInterval is % d”, interval);

//time interval since today to other date:

NSTimeInterval interval2 =
[ aDate timeInterval Since Now];
Ns log (@ “ the time interval is %d , interval2);

// how to add a time interval to the date object

     NSDate * addedDate =[aDate  dateByAddingTimeInterval : 2x24x60x60];
NSLog (@ “ the time interval is % d”, addedDate);

NS timer:-

-(void) view DidLoad
    Count =0;
  ATimer  =  [  NSTimer ScheduledTimerWithTimeInterval:1
                         Target   :  self
                         Selector : @ selector (timer method)
                       User info :  nil
                       Repeats   : yes];
[ super viewDidLoad];
(void)  timer method
  NSLog (@  “timer method called”,)
If (count = = 5)
    [aTimer invalidate];


0 Responses on iPhone : Exploring the Foundation Framework – NS Date & Timer Class"

Leave a Message

Your email address will not be published. Required fields are marked *

Copy Rights Reserved © Mindmajix.com All rights reserved. Disclaimer.