<!--
{
  "availability" : [
    "iOS: 2.0.0 -",
    "iPadOS: 2.0.0 -",
    "macCatalyst: 13.0.0 -",
    "macOS: 10.0.0 -",
    "tvOS: 9.0.0 -",
    "visionOS: 1.0.0 -",
    "watchOS: 2.0.0 -"
  ],
  "documentType" : "symbol",
  "framework" : "Foundation",
  "identifier" : "/documentation/Foundation/NSCalendar/components(_:from:)",
  "metadataVersion" : "0.1.0",
  "role" : "Instance Method",
  "symbol" : {
    "kind" : "Instance Method",
    "modules" : [
      "Foundation"
    ],
    "preciseIdentifier" : "c:objc(cs)NSCalendar(im)components:fromDate:"
  },
  "title" : "components(_:from:)"
}
-->

# components(_:from:)

Returns the date components representing a given date.

```
func components(_ unitFlags: NSCalendar.Unit, from date: Date) -> DateComponents
```

## Parameters

`unitFlags`

The components into which to decompose `date`.

`date`

The date for which to perform the calculation.

## Return Value

An `NSDateComponents` object containing `date` decomposed into the components specified by `unitFlags`. Returns `nil` if `date` falls outside of the defined range of the receiver or if the computation cannot be performed.

## Discussion

The Weekday ordinality, when requested, refers to the next larger (than Week) of the requested units. Some computations can take a relatively long time.

The following example shows how to use this method to determine the current year, month, and day, using an existing calendar (`gregorian`):

```objc
unsigned unitFlags = NSYearCalendarUnit | NSMonthCalendarUnit |  NSDayCalendarUnit;
NSDate *date = [NSDate date];
NSDateComponents *comps = [gregorian components:unitFlags fromDate:date];
```

## See Also

[`-  dateFromComponents:`](/documentation/Foundation/NSCalendar/date(from:))

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

[`-  dateByAddingComponents:toDate:options:`](/documentation/Foundation/NSCalendar/date(byAdding:to:options:))

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



---

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)