Home / iPhone

Exploring the Foundation Framework in iPhone - NS Date & Timer Class

Rating: 4.0Blog-star
Views: 1767
by Ravindra Savaram
Last modified: August 10th 2020


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).

Inclined to build a profession as IOS Developer? Then here is the blog post on iPhone Development Certification Training.

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];


Frequently Asked iPhone Interview Questions & Answers


About Author

NameRavindra Savaram
Author Bio


Ravindra Savaram is a Content Lead at Mindmajix.com. His passion lies in writing articles on the most popular IT platforms including Machine learning, DevOps, Data Science, Artificial Intelligence, RPA, Deep Learning, and so on. You can stay up to date on all these technologies by following him on LinkedIn and Twitter.