Contents

date(from:)

Returns a date representing the absolute time calculated from given components.

Declaration

func date(from comps: DateComponents) -> Date?

Parameters

  • comps:

    The components from which to calculate the returned date.

Return Value

A new NSDate object representing the absolute time calculated from comps. Returns nil if the receiver cannot convert the components given in comps into an NSDate object.

Discussion

When there are insufficient components provided to completely specify an absolute time, a calendar uses default values of its choice. When there is inconsistent information, a calendar may ignore some of the components parameters or the method may return nil. Unnecessary components are ignored (for example, Day takes precedence over Weekday and Weekday ordinals).

The following example shows how to use this method to create a date object to represent 14:10:00 on 6 January 1965, for a given calendar (gregorian).

NSDateComponents *comps = [[NSDateComponents alloc] init];
[comps setYear:1965];
[comps setMonth:1];
[comps setDay:6];
[comps setHour:14];
[comps setMinute:10];
[comps setSecond:0];
NSDate *date = [gregorian dateFromComponents:comps];
[comps release];

Note that some computations can take a relatively long time to perform.

See Also

Related Documentation

Calculating Dates