Dates and Times in Objective-C
Last Updated :
26 Apr, 2025
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.
Similar Reads
Dates and Time in MATLAB
MATLAB provides many ways to deal with date and time in form of DateTime, calendar duration, and duration data types. These data types do not only support storing and representing date-times but, also allow operations on date time. We shall look at these three data types separately. DateTimeThe date
2 min read
SQLite Date and Time
In the world of database management, understanding how to handle dates and times is important. SQLite is a popular relational database management system that offers a robust set of functions and features for working with date and time data. From storing timestamps to performing date calculations, SQ
5 min read
Adding Dates and Times in Python
Prerequisite: Python â datetime.tzinfo() Python supports datetime module to perform manipulations for date and time. Datetime module allows the user to perform various date and time-related arithmetic operations, extraction of various time frames and formatting of output in different formats. Object
3 min read
Ruby Date and Time
The class Time and Date in Ruby deals with the dates and times. It helps us to get the current time and date according to our system configurations. It also gives us the components of a date and time and also to format time and date. The date is the day of a month or year that is represented by a nu
3 min read
Perl | Date and Time
Perl is one of the most feature-rich programming languages. Perl is portable and cross-platform. Perl can run on over 100 platforms with highly integrated text processing ability. Perl contains the features of different languages like C, sed, awk, and sh etc. which makes the Perl more useful and pro
3 min read
How to merge date and time in R?
The date and time objects in R programming language can be represented using character strings in R. The date and time objects can be merged together using POSIX format or in the form of datetime objects. The POSIXlt class stores date and time information. Discussed below are various approaches to c
3 min read
Convert UNIX Timestamp to Date Object in R
UNIX timestamp refers to the number of seconds that have elapsed since the epoch. The timestamp object is not easily understandable and should be converted to other user-friendly formats. The Date objects in R Programming Language can be used to display the specified timestamp in a crisp way. Date o
3 min read
How to get the current date and time in seconds ?
In this article, we will learn how to get the current date & time in seconds using JavaScript build-in methods. We will perform it in 2 ways: Using Date.now() methodUsing new Date.getTime() methodMethod 1: Using Date.now() MethodThe Date.now() method returns the number of milliseconds elapsed si
2 min read
Working with Date and Time in Julia
Julia provides a library to use Dates for dealing with date and time. The Dates module comes inbuilt with the Julia we just need to import it in order to use it. Now we need to prefix every function with an explicit type Dates, e.g Dates.Date. If you donât want to prefix on each function just add us
5 min read
Working with Datetime Objects and Timezones in Python
In this article, we are going to work with Datetime objects and learn about their behavior when Time zones are introduced. We are going to be working with the Python datetime module. Getting a Datetime objectMethod 1: Using now() method A very easy way to get a Datetime object is to use the datetime
5 min read