<!--
{
  "availability" : [
    "iOS: 18.0.0 -",
    "iPadOS: 18.0.0 -",
    "macCatalyst: 18.0.0 -",
    "macOS: 15.0.0 -",
    "tvOS: 18.0.0 -",
    "visionOS: 2.0.0 -",
    "watchOS: 11.0.0 -"
  ],
  "documentType" : "symbol",
  "framework" : "SwiftUI",
  "identifier" : "/documentation/SwiftUI/SystemFormatStyle/DateOffset/init(to:allowedFields:maxFieldCount:sign:)",
  "metadataVersion" : "0.1.0",
  "role" : "Initializer",
  "symbol" : {
    "kind" : "Initializer",
    "modules" : [
      "SwiftUI"
    ],
    "preciseIdentifier" : "s:7SwiftUI17SystemFormatStyleO10DateOffsetV2to13allowedFields13maxFieldCount4signAE10Foundation0F0V_ShyAL010ComponentsdE0V0L0VGSiAJ06NumberdE13ConfigurationO19SignDisplayStrategyVtcfc"
  },
  "title" : "init(to:allowedFields:maxFieldCount:sign:)"
}
-->

# init(to:allowedFields:maxFieldCount:sign:)

Creates a format style that displays the offset between a
comparison date and an anchor date that you provide.

```
init(to anchor: Date, allowedFields: Set<Date.ComponentsFormatStyle.Field> = [.year, .month, .week, .day, .hour, .minute, .second], maxFieldCount: Int = 2, sign: NumberFormatStyleConfiguration.SignDisplayStrategy = .automatic)
```

## Parameters

`anchor`

The date the style uses to calculate the offset from
the format input date.

`allowedFields`

The units of time that may appear in the
formatted output.

`maxFieldCount`

The maximum number of units shown at
once. For example, 1 hour, 34 minutes, and 23 seconds
is shown as `1 hour, 34 minutes` by default, but as
`1 hour` if `maxFieldCount` is set to 1.

`sign`

The strategy for displaying a sign to signal
whether the offset points toward the future or past.

## Discussion

The style uses a time-pattern representation (`3:46`) when
`allowedFields` contains only `.minute` and `.second`, or
contains `.hour`, `.minute`, and `.second`. For any other
combination of fields, it uses calendar units like
`3 months, 11 days`.

---

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)