<!--
{
  "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/NSDateComponents/setValue(_:forComponent:)",
  "metadataVersion" : "0.1.0",
  "role" : "Instance Method",
  "symbol" : {
    "kind" : "Instance Method",
    "modules" : [
      "Foundation"
    ],
    "preciseIdentifier" : "c:objc(cs)NSDateComponents(im)setValue:forComponent:"
  },
  "title" : "setValue(_:forComponent:)"
}
-->

# setValue(_:forComponent:)

Sets a value for a given calendar unit.

```
func setValue(_ value: Int, forComponent unit: NSCalendar.Unit)
```

## Parameters

`value`

The value to set for the `unit` component.

`unit`

The calendar unit for which to set `value`. Do not pass [`calendar`](/documentation/Foundation/NSCalendar/Unit/calendar) or [`timeZone`](/documentation/Foundation/NSCalendar/Unit/timeZone).

## Discussion

This method allows for component values to be set for an [`NSCalendar.Unit`](/documentation/Foundation/NSCalendar/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)