Instance Method

dateBySettingUnit:value:ofDate:options:

Returns a new date representing the date calculated by setting a specific component of a given date to a given value, while trying to keep lower components the same.

Declaration

- (NSDate *)dateBySettingUnit:(NSCalendarUnit)unit value:(NSInteger)v ofDate:(NSDate *)date options:(NSCalendarOptions)opts;

Parameters

unit

The unit to set with the given value. For possible values, see NSCalendarUnit.

v

The value to set for the given calendar unit.

date

The date to use to perform the calculation.

opts

Options for the calculation. For possible values, see NSCalendarOptions.

Return Value

A new NSDate instance representing the date calculated by setting a specific component of a given date to a given value. If the unit already has that value, this may result in a date which is the same as the given date. If no such time exists for the specified components, the next available date is returned, which may be on a different calendar day.

Discussion

Changing a component's value often requires higher or coupled components to change as well. For example, setting the weekday to "Thursday" will require the day component to change its value, and possibly the month and year as well. You can use the nextDateAfterDate:matchingUnit:value:options: method to specify more precise behavior for determining the next or previous date for a given date component.

See Also

Calculating Dates

- dateFromComponents:

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

- dateByAddingComponents:toDate:options:

Returns a date representing the absolute time calculated by adding given components to a given date.

- dateByAddingUnit:value:toDate:options:

Returns a date representing the absolute time calculated by adding the value of a given component to a given date.

- dateBySettingHour:minute:second:ofDate:options:

Creates a new date calculated with the given time.

- dateWithEra:year:month:day:hour:minute:second:nanosecond:

Returns a date created with the given components.

- dateWithEra:yearForWeekOfYear:weekOfYear:weekday:hour:minute:second:nanosecond:

Returns a new date created with the given components base on a week-of-year value.

- nextWeekendStartDate:interval:options:afterDate:

Returns by reference the starting date and time interval range of the next weekend period after a given date.