Open In App

Dates and Times in Objective-C

Last Updated : 17 Apr, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

In Objective-C, dates and times are represented using the NSDate and NSDateFormatter classes. NSDate class represents a single point in time. It stores an absolute point in time, independent of any particular calendar or time zone. You can use NSDate to represent a date and time, or just a date or time by itself. To create a new NSDate object, you can use the date class method of NSDate, which returns the current date and time. 

Syntax:

NSDate *currentDate = [NSDate date];

Example:

ObjectiveC




// Objective-C program to find current date and time
#import <Foundation/Foundation.h>
 
int main (int argc, const char * argv[])
{
   NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
 
   NSDate *currentDate = [NSDate date];
         
   // Print the current date and time using the default formatting
   NSLog(@"Current date and time: %@", currentDate);
   [pool drain];
   return 0;
}


Output: 

Output -1

 

You can also create an NSDate object for a specific point in time using the dateWithTimeIntervalSinceReferenceDate: method, which takes a time interval as an argument and returns an NSDate object representing the point in time that is the specified time interval from the reference date (which is January 1, 2001).

Example:

ObjectiveC




// Objective-C program to print date and time
#import <Foundation/Foundation.h>
 
int main (int argc, const char * argv[])
{
   NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
 
   NSDate *date = [NSDate dateWithTimeIntervalSinceReferenceDate:0];
         
   // Creates and returns an NSDate object set to
   // a given number of seconds from the first
   // instant of 1 January 2001, GMT.
   NSLog(@"%@", date);
   [pool drain];
   return 0;
}


Output: 

Output 2

 

Comparing two Dates

You can also use the compare: method to compare two NSDate objects and determine which one comes before or after the other. This method returns an NSComparisonResult value indicating whether the first NSDate object is less than, equal to, or greater than the second.

ObjectiveC




// Objective-C program to comparing two dates
#import <Foundation/Foundation.h>
 
int main (int argc, const char * argv[])
{
   NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
 
   NSDate *date = [NSDate dateWithTimeIntervalSinceReferenceDate:0];
    
   // Returns an NSComparisonResult value that
   // indicates the temporal ordering of the
   // receiver and another given date.
   NSLog(@"%d", [date compare:[NSDate date]]);
   [pool drain];
   return 0;
}


Output: 

Output 3

 

Displaying the Date and Time in the Specific Format

To display a date or time in a specific format, you can use the NSDateFormatter class. This class allows you to specify a template for the date and time format you want to use, and it will generate a string representation of the date or time using that format.

Example 1:

In this example, we are going to display the current date and time in the format “2021-01-01”.

ObjectiveC




// Objective-C program to display date and time in the specified format
#import <Foundation/Foundation.h>
 
int main() {
   NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
   NSDate *date= [NSDate date];
   NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
    
   // Setting the format
   [dateFormatter setDateFormat:@"yyyy-MM-dd"];
    
   // Displaying the current format
   NSString *dateString = [dateFormatter stringFromDate:date];
   NSLog(@"Current date is %@",dateString);
    
    // Displaying the new format
   NSDate *newDate = [dateFormatter dateFromString:dateString];
   NSLog(@"NewDate: %@",newDate);
    
   [pool drain];
   return 0;
}


Output: 

Output 4

 

Example 2:

In this example, we are going to display the current date and time in the format “Sunday, June 10, 2021 at 10:30 AM”.

ObjectiveC




// Objective-C program to display date and time in the specified format
#import <Foundation/Foundation.h>
 
int main (int argc, const char * argv[])
{
   NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
 
   NSDate *currentDate = [NSDate date];
         
   // Print the current date and time using the default formatting
   NSLog(@"Current date and time: %@", currentDate);
    
   // Create a date formatter to print the date in a specific format
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"EEEE, MMMM d, yyyy 'at' h:mm a"];
     
    // Use the date formatter to print the date in the desired format
    NSLog(@"Formatted date and time: %@", [formatter stringFromDate:currentDate]);
   [pool drain];
   return 0;
}


Output: 

Output 5

 

In addition to NSDate and NSDateFormatter, the Foundation framework also provides several other classes for working with dates and times, such as NSCalendar, NSTimeZone, and NSLocale. These classes allow you to perform more advanced operations such as converting between different calendars and time zones, and formatting dates and times according to the conventions of a specific region or language.



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads