Dates and Times in Objective-C
Last Updated :
17 Apr, 2023
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
#import <Foundation/Foundation.h>
int main ( int argc, const char * argv[])
{
NSAutoreleasePool * pool = [[ NSAutoreleasePool alloc] init];
NSDate *currentDate = [ NSDate date];
NSLog ( @"Current date and time: %@" , currentDate);
[pool drain];
return 0;
}
|
Output:
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
#import <Foundation/Foundation.h>
int main ( int argc, const char * argv[])
{
NSAutoreleasePool * pool = [[ NSAutoreleasePool alloc] init];
NSDate *date = [ NSDate dateWithTimeIntervalSinceReferenceDate:0];
NSLog ( @"%@" , date);
[pool drain];
return 0;
}
|
Output:
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
#import <Foundation/Foundation.h>
int main ( int argc, const char * argv[])
{
NSAutoreleasePool * pool = [[ NSAutoreleasePool alloc] init];
NSDate *date = [ NSDate dateWithTimeIntervalSinceReferenceDate:0];
NSLog ( @"%d" , [date compare:[ NSDate date]]);
[pool drain];
return 0;
}
|
Output:
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
#import <Foundation/Foundation.h>
int main() {
NSAutoreleasePool * pool = [[ NSAutoreleasePool alloc] init];
NSDate *date= [ NSDate date];
NSDateFormatter *dateFormatter = [[ NSDateFormatter alloc]init];
[dateFormatter setDateFormat: @"yyyy-MM-dd" ];
NSString *dateString = [dateFormatter stringFromDate:date];
NSLog ( @"Current date is %@" ,dateString);
NSDate *newDate = [dateFormatter dateFromString:dateString];
NSLog ( @"NewDate: %@" ,newDate);
[pool drain];
return 0;
}
|
Output:
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
#import <Foundation/Foundation.h>
int main ( int argc, const char * argv[])
{
NSAutoreleasePool * pool = [[ NSAutoreleasePool alloc] init];
NSDate *currentDate = [ NSDate date];
NSLog ( @"Current date and time: %@" , currentDate);
NSDateFormatter *formatter = [[ NSDateFormatter alloc] init];
[formatter setDateFormat: @"EEEE, MMMM d, yyyy 'at' h:mm a" ];
NSLog ( @"Formatted date and time: %@" , [formatter stringFromDate:currentDate]);
[pool drain];
return 0;
}
|
Output:
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.
Share your thoughts in the comments
Please Login to comment...