---
title: "date(bySettingUnit:value:of:options:)"
framework: foundation
role: symbol
role_heading: Instance Method
path: "foundation/nscalendar/date(bysettingunit:value:of:options:)"
---

# date(bySettingUnit:value:of: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

```swift
func date(bySettingUnit unit: NSCalendar.Unit, value v: Int, of date: Date, options opts: NSCalendar.Options = []) -> Date?
```

## Parameters

- `unit`: The unit to set with the given value. For possible values, see doc://com.apple.foundation/documentation/Foundation/NSCalendar/Unit.
- `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 doc://com.apple.foundation/documentation/Foundation/NSCalendar/Options.

## Return Value

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

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 nextDate(after:matching:value:options:) method to specify more precise behavior for determining the next or previous date for a given date component.

## See Also

### Related Documentation

- [nextDate(after:matching:value:options:)](foundation/nscalendar/nextdate(after:matching:value:options:).md)

### Calculating Dates

- [date(from:)](foundation/nscalendar/date(from:).md)
- [date(byAdding:to:options:)](foundation/nscalendar/date(byadding:to:options:).md)
- [date(byAdding:value:to:options:)](foundation/nscalendar/date(byadding:value:to:options:).md)
- [date(bySettingHour:minute:second:of:options:)](foundation/nscalendar/date(bysettinghour:minute:second:of:options:).md)
- [date(era:year:month:day:hour:minute:second:nanosecond:)](foundation/nscalendar/date(era:year:month:day:hour:minute:second:nanosecond:).md)
- [date(era:yearForWeekOfYear:weekOfYear:weekday:hour:minute:second:nanosecond:)](foundation/nscalendar/date(era:yearforweekofyear:weekofyear:weekday:hour:minute:second:nanosecond:).md)
- [nextWeekendStart(_:interval:options:after:)](foundation/nscalendar/nextweekendstart(_:interval:options:after:).md)
