Creating and Initializing Date Objects

+ date

Creates and returns a new date set to the current date and time.

Example

NSDate *date = [NSDate date];
NSLog(@"%@", date);
		

Output

2014-04-09 06:32:49.297 iOS-Tutorial[3798:a0b] 2014-04-09 01:02:49 +0000
		

+ dateWithTimeIntervalSinceNow:

Creates and returns an NSDate object set to a given number of seconds from the current date and time.

Example

NSDate *date = [NSDate dateWithTimeIntervalSinceNow:100000000];
NSLog(@"%@", date);
		

Output

2014-04-09 06:34:25.363 iOS-Tutorial[3845:a0b] 2017-06-09 10:51:05 +0000
		

+ dateWithTimeInterval:sinceDate:

Creates and returns an NSDate object set to a given number of seconds from the specified date.

Example

NSDate *date = [NSDate dateWithTimeInterval:100000000 sinceDate:[NSDate date]];
NSLog(@"%@", date);
		

Output

2014-04-09 06:35:23.165 iOS-Tutorial[3860:a0b] 2017-06-09 10:52:03 +0000
		

+ dateWithTimeIntervalSinceReferenceDate:

Creates and returns an NSDate object set to a given number of seconds from the first instant of 1 January 2001, GMT.

Example

NSDate *date = [NSDate dateWithTimeIntervalSinceReferenceDate:0];
NSLog(@"%@", date);
		

Output

2014-04-09 06:36:31.390 iOS-Tutorial[3884:a0b] 2001-01-01 00:00:00 +0000
		

+ dateWithTimeIntervalSince1970:

Creates and returns an NSDate object set to the given number of seconds from the first instant of 1 January 1970, GMT.

Example

NSDate *date = [NSDate dateWithTimeIntervalSince1970:0];
NSLog(@"%@", date);
		

Output

2014-04-09 06:37:07.185 iOS-Tutorial[3894:a0b] 1970-01-01 00:00:00 +0000
		

- init

Returns an NSDate object initialized to the current date and time.

Example

NSDate *date = [[NSDate alloc]init];
NSLog(@"%@", date);
		

Output

2014-04-09 06:37:43.270 iOS-Tutorial[3906:a0b] 2014-04-09 01:07:43 +0000
		

- initWithTimeIntervalSinceNow:

Returns an NSDate object initialized relative to the current date and time by a given number of seconds.

Example

NSDate *date = [[NSDate alloc]initWithTimeIntervalSinceNow:0];
NSLog(@"%@", date);
		

Output

2014-04-09 06:38:11.918 iOS-Tutorial[3918:a0b] 2014-04-09 01:08:11 +0000
		

- initWithTimeInterval:sinceDate:

Returns an NSDate object initialized relative to another given date by a given number of seconds.

Example

NSDate *date = [[NSDate alloc]initWithTimeInterval:0 sinceDate:[NSDate date]];
NSLog(@"%@", date);
		

Output

2014-04-09 06:38:59.912 iOS-Tutorial[3930:a0b] 2014-04-09 01:08:59 +0000
		

- initWithTimeIntervalSinceReferenceDate:

Returns an NSDate object initialized relative the first instant of 1 January 2001, GMT by a given number of seconds.

Example

NSDate *date = [[NSDate alloc]initWithTimeIntervalSinceReferenceDate:0];
NSLog(@"%@", date);
		

Output

2014-04-09 06:39:41.531 iOS-Tutorial[3941:a0b] 2001-01-01 00:00:00 +0000
		

- initWithTimeIntervalSince1970:

Returns an NSDate object set to the given number of seconds from the first instant of 1 January 1970, GMT.

Example

NSDate *date = [[NSDate alloc]initWithTimeIntervalSince1970:0];
NSLog(@"%@", date);
		

Output

2014-04-09 06:40:06.432 iOS-Tutorial[3953:a0b] 1970-01-01 00:00:00 +0000
		

Getting Temporal Boundaries

+ distantFuture

Creates and returns an NSDate object representing a date in the distant future.

Example

NSDate *date = [NSDate distantFuture];
NSLog(@"%@", date);
		

Output

2014-04-09 06:40:46.128 iOS-Tutorial[3967:a0b] 4001-01-01 00:00:00 +0000
		

+ distantPast

Creates and returns an NSDate object representing a date in the distant past.

Example

NSDate *date = [NSDate distantPast];
NSLog(@"%@", date);
		

Output

2014-04-09 06:41:17.550 iOS-Tutorial[3979:a0b] 0001-12-30 00:00:00 +0000
		

Comparing Dates

- isEqualToDate:

Returns a Boolean value that indicates whether a given object is an NSDate object and exactly equal the receiver.

Example

NSDate *date = [NSDate distantPast];
NSLog(@"%d", [date isEqualToDate:[NSDate date]]);
		

Output

2014-04-09 06:42:08.488 iOS-Tutorial[3992:a0b] 0
		

- earlierDate:

Returns the earlier of the receiver and another given date.

Example

NSDate *date = [NSDate dateWithTimeIntervalSinceReferenceDate:0];
NSLog(@"%@", [date earlierDate:[NSDate date]]);
		

Output

2014-04-09 06:47:08.442 iOS-Tutorial[4073:a0b] 2001-01-01 00:00:00 +0000
		

- laterDate:

Returns the later of the receiver and another given date.

Example

NSDate *date = [NSDate dateWithTimeIntervalSinceReferenceDate:0];
NSLog(@"%@", [date laterDate:[NSDate date]]);
		

Output

2014-04-09 06:48:00.957 iOS-Tutorial[4089:a0b] 2014-04-09 01:18:00 +0000
		

- compare:

Returns an NSComparisonResult value that indicates the temporal ordering of the receiver and another given date.

Example

NSDate *date = [NSDate dateWithTimeIntervalSinceReferenceDate:0];
NSLog(@"%d", [date compare:[NSDate date]]);
		

Output

2014-04-09 06:48:32.369 iOS-Tutorial[4101:a0b] -1
		

Getting Time Intervals

- timeIntervalSinceDate:

Returns the interval between the receiver and another given date.

Example

NSDate *date = [NSDate dateWithTimeIntervalSinceReferenceDate:0];
NSLog(@"%f", [date timeIntervalSinceDate:[NSDate date]]);
		

Output

2014-04-09 06:49:40.281 iOS-Tutorial[4113:a0b] -418699180.281044
		

- timeIntervalSinceNow

Returns the interval between the receiver and the current date and time.

Example

NSDate *date = [NSDate dateWithTimeIntervalSinceReferenceDate:0];
NSLog(@"%f", [date timeIntervalSinceNow]);
		

Output

2014-04-09 06:50:13.842 iOS-Tutorial[4126:a0b] -418699213.842396
		

+ timeIntervalSinceReferenceDate

Returns the interval between the first instant of 1 January 2001, GMT and the current date and time.

Example

NSLog(@"%f", [NSDate timeIntervalSinceReferenceDate]);
		

Output

2014-04-09 06:50:53.093 iOS-Tutorial[4138:a0b] 418699253.093661
		

- timeIntervalSinceReferenceDate

Returns the interval between the receiver and the first instant of 1 January 2001, GMT.

Example

NSDate *date = [NSDate dateWithTimeIntervalSinceReferenceDate:0];
NSLog(@"%f", [date timeIntervalSinceReferenceDate]);
		

Output

2014-04-09 06:51:25.467 iOS-Tutorial[4150:a0b] 0.000000
		

- timeIntervalSince1970

Returns the interval between the receiver and the first instant of 1 January 1970, GMT.

Example

NSDate *date = [NSDate dateWithTimeIntervalSinceReferenceDate:0];
NSLog(@"%f", [date timeIntervalSince1970]);
		

Output

2014-04-09 06:51:49.584 iOS-Tutorial[4162:a0b] 978307200.000000
		

Adding a Time Interval

- dateByAddingTimeInterval:

Returns a new NSDate object that is set to a given number of seconds relative to the receiver.

Example

NSDate *date = [NSDate dateWithTimeIntervalSinceReferenceDate:0];
NSLog(@"%@", [date dateByAddingTimeInterval:10000000]);
		

Output

2014-04-09 06:52:55.330 iOS-Tutorial[4198:a0b] 2001-04-26 17:46:40 +0000
		

Representing Dates as Strings

- description

Returns a string representation of the receiver.

Example

NSDate *date = [NSDate dateWithTimeIntervalSinceReferenceDate:0];
NSLog(@"%@", [date description]);
		

Output

2014-04-09 06:53:33.976 iOS-Tutorial[4211:a0b] 2001-01-01 00:00:00 +0000
		

- descriptionWithLocale:

Returns a string representation of the receiver using the given locale.

Example

NSDate *date = [NSDate dateWithTimeIntervalSinceReferenceDate:0];
NSLog(@"%@", [date descriptionWithLocale:[NSLocale systemLocale]]);
		

Output

2014-04-09 06:54:05.887 iOS-Tutorial[4224:a0b] 2001 Month1 1, Mon 05:30:00 GMT+05:30
		

Advertisements