<!--
{
  "availability" : [
    "iOS: 8.0.0 -",
    "iPadOS: 8.0.0 -",
    "macCatalyst: 13.1.0 -",
    "macOS: 10.9.0 -",
    "tvOS: 9.0.0 -",
    "visionOS: 1.0.0 -",
    "watchOS: 2.0.0 -"
  ],
  "documentType" : "symbol",
  "framework" : "Foundation",
  "identifier" : "/documentation/Foundation/NSCalendar/date(bySettingUnit:value:of:options:)",
  "metadataVersion" : "0.1.0",
  "role" : "Instance Method",
  "symbol" : {
    "kind" : "Instance Method",
    "modules" : [
      "Foundation"
    ],
    "preciseIdentifier" : "c:objc(cs)NSCalendar(im)dateBySettingUnit:value:ofDate:options:"
  },
  "title" : "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.

```
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 [`NSCalendar.Unit`](/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 [`NSCalendar.Options`](/documentation/Foundation/NSCalendar/Options).

## 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 [`nextDate(after:matching:value:options:)`](/documentation/Foundation/NSCalendar/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

[`-  nextDateAfterDate:matchingUnit:value:options:`](/documentation/Foundation/NSCalendar/nextDate(after:matching:value:options:))

Returns the next date after a given date matching the given calendar unit value.



---

Copyright &copy; 2026 Apple Inc. All rights reserved. | [Terms of Use](https://www.apple.com/legal/internet-services/terms/site.html) | [Privacy Policy](https://www.apple.com/privacy/privacy-policy)