---
title: "components(style:fields:)"
framework: foundation
role: symbol
role_heading: Type Method
path: "foundation/formatstyle/components(style:fields:)"
---

# components(style:fields:)

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

## Declaration

```swift
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 doc://com.apple.foundation/documentation/Foundation/Date/ComponentsFormatStyle/Style-swift.struct/abbreviated or doc://com.apple.foundation/documentation/Foundation/Date/ComponentsFormatStyle/Style-swift.struct/narrow.
- `fields`: A set of date component fields to include in the formatted output.

## Return Value

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

## Discussion

Discussion Use this type method when the call point allows the use of Date.ComponentsFormatStyle. You typically do this when calling the formatted(_:) method of a Range<Date>. The following example creates a 120-day date range, and then uses a Date.ComponentsFormatStyle to express this as a count of weeks and days: 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

## See Also

### Applying date and time styles

- [dateTime](foundation/formatstyle/datetime.md)
- [Date.FormatStyle](foundation/date/formatstyle.md)
- [Date.ISO8601FormatStyle](foundation/date/iso8601formatstyle.md)
- [verbatim(_:locale:timeZone:calendar:)](foundation/formatstyle/verbatim(_:locale:timezone:calendar:).md)
- [Date.VerbatimFormatStyle](foundation/date/verbatimformatstyle.md)
- [interval](foundation/formatstyle/interval.md)
- [Date.IntervalFormatStyle](foundation/date/intervalformatstyle.md)
- [relative(presentation:unitsStyle:)](foundation/formatstyle/relative(presentation:unitsstyle:).md)
- [Date.RelativeFormatStyle](foundation/date/relativeformatstyle.md)
- [Date.ComponentsFormatStyle](foundation/date/componentsformatstyle.md)
