<!--
{
  "availability" : [
    "iOS: 15.0.0 -",
    "iPadOS: 15.0.0 -",
    "macCatalyst: 15.0.0 -",
    "macOS: 12.0.0 -",
    "tvOS: 15.0.0 -",
    "visionOS: 1.0.0 -",
    "watchOS: 8.0.0 -"
  ],
  "documentType" : "symbol",
  "framework" : "Foundation",
  "identifier" : "/documentation/Foundation/FormatStyle/components(style:fields:)",
  "metadataVersion" : "0.1.0",
  "role" : "Type Method",
  "symbol" : {
    "kind" : "Type Method",
    "modules" : [
      "Foundation"
    ],
    "preciseIdentifier" : "s:10Foundation11FormatStylePA2A4DateV010ComponentsbC0VRszrlE10components5style6fieldsA2G0C0V_ShyAG5FieldVGSgtFZ"
  },
  "title" : "components(style:fields:)"
}
-->

# components(style:fields:)

Returns a style for formatting a date interval in terms of specific date components.

```
static func components(style: Date.ComponentsFormatStyle.Style, fields: Set<Date.ComponentsFormatStyle.Field>? = nil) -> Self
```

## Parameters

`style`

The style to use for the fields, such as [`abbreviated`](/documentation/Foundation/Date/ComponentsFormatStyle/Style-swift.struct/abbreviated) or [`narrow`](/documentation/Foundation/Date/ComponentsFormatStyle/Style-swift.struct/narrow).

`fields`

A set of date component fields to include in the formatted output.

## Return Value

A date format style that uses the specified style and fields.

## Discussion

Use this type method when the call point allows the use of [`Date.ComponentsFormatStyle`](/documentation/Foundation/Date/ComponentsFormatStyle). You typically do this when calling the <doc://com.apple.documentation/documentation/Swift/Range/formatted(_:)> method of a `Range<Date>`.

The following example creates a 120-day date range, and then uses a [`Date.ComponentsFormatStyle`](/documentation/Foundation/Date/ComponentsFormatStyle) to express this as a count of weeks and days:

```swift
let date = Date()
let futureDate = Calendar.current.date(byAdding: .day, value: 120, to: date)!
let interval = (date..<futureDate)
let formatted = interval.formatted(
    .components(style: .wide,
                fields: [.week, .day])) // 17 weeks, 1 day
```

---

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)