# Relative date style of \`Date.FormatStyle.DateStyle\`

**URL:** <https://forums.swift.org/t/relative-date-style-of-date-formatstyle-datestyle/63091>\
**Category:** Using Swift\
**Created:** [February 12, 2023, 4:58pm UTC](https://forums.swift.org/t/relative-date-style-of-date-formatstyle-datestyle/63091 "2023-02-12T16:58:45Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Doug](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/doug/32/7679_2.png) [@Doug](https://forums.swift.org/u/Doug)\
**Post date:** [February 12, 2023, 4:58pm UTC](https://forums.swift.org/t/relative-date-style-of-date-formatstyle-datestyle/63091/1 "2023-02-12T16:58:45Z")

</div>

Whilst updating a project that I haven't looked at in a while I've been removing the various `Formatters` in the project and replacing them with the newer `formatted()` method which overall seems to have been fairly simple. However there's one case where I'm pretty stuck:

```swift
let formatter = DateFormatter()
formatter.timeStyle = .short
formatter.dateStyle = .short
formatter.doesRelativeDateFormatting = true

```

I can get the equivalent style from `formatted` for distant dates:

```swift
let myStyle = Date.FormatStyle(date: .numeric, time: .shortened).year(.twoDigits)

let newYearsMidday = Calendar.current.date(from: DateComponents(year: 2023, month: 1, day: 1, hour: 12, minute: 0))!

formatter.string(from: newYearsMidday) // "1/1/23, 12:00 PM"
newYearsMidday.formatted(myStyle) // "1/1/23, 12:00 PM"

```

But I'm stumped about how to do it for closer dates that should use the relative date format:

```swift
formatter.string(from: .now) // "Today, 4:54 PM"
Date.now.formatted(myStyle) // "2/12/23, 4:54 PM"

```

Is this possible with the new API or does it require sticking with `DateFormatter`?

---

<div class="post-metadata">

**Author:** ![tera](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/tera/32/38201_2.png) [@tera](https://forums.swift.org/u/tera)\
**Post date:** [February 12, 2023, 6:06pm UTC](https://forums.swift.org/t/relative-date-style-of-date-formatstyle-datestyle/63091/2 "2023-02-12T18:06:40Z")

</div>

Look [here](https://developer.apple.com/documentation/foundation/date/relativeformatstyle).

---

<div class="post-metadata">

**Author:** ![Doug](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/doug/32/7679_2.png) [@Doug](https://forums.swift.org/u/Doug)\
**Post date:** [February 12, 2023, 6:18pm UTC](https://forums.swift.org/t/relative-date-style-of-date-formatstyle-datestyle/63091/3 "2023-02-12T18:18:01Z")

</div>

I don't think that `RelativeDateFormat` is quite right here though?

E.g for that last example

```swift
let relativeStyle = Date.RelativeFormatStyle(presentation: .named, capitalizationContext: .standalone)

formatter.string(from: .now) // "Today, 4:54 PM"
Date.now.formatted(relativeStyle) // "now"

```

`RelativeFormatStyle` is the equivalent to `RelativeDateTimeFormatter`, whereas `DateFormatter` could apply relative formatting for [just the date component](https://developer.apple.com/documentation/foundation/dateformatter/1415848-doesrelativedateformatting) where appropriate.

---

<div class="post-metadata">

**Author:** ![tera](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/tera/32/38201_2.png) [@tera](https://forums.swift.org/u/tera)\
**Post date:** [February 12, 2023, 6:27pm UTC](https://forums.swift.org/t/relative-date-style-of-date-formatstyle-datestyle/63091/4 "2023-02-12T18:27:55Z")

</div>

No idea without a further research. Perhaps just go through all parameter permutations to see if you can find a way. I remember @young was playing with those new style formatters recently, and @eskimo might have the answer off-hand.

---

<div class="post-metadata">

**Author:** ![young](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/young/32/7878_2.png) [@young](https://forums.swift.org/u/young)\
**Post date:** [February 13, 2023, 7:03pm UTC](https://forums.swift.org/t/relative-date-style-of-date-formatstyle-datestyle/63091/5 "2023-02-13T19:03:09Z")

</div>

> [@New Date.FormatStyle: Anyway to do 24 hour?](https://forums.swift.org/t/new-date-formatstyle-anyway-to-do-24-hour/52994/42):
>
> Do you mean look into the AttributedString, find the AM/PM range, if it's "PM", change the hour range to twenty four hour value. Finally remove the AM/PM range? (but what about any space in from of AM/PM? How to get at that and also remove it?) I dump out all the runs, the separator and space is Foundation.AttributedString.\_InternalRun, so just remove the one preceding AM/PM. Edit: here is what I come up with: // VerbatimHour option is Foundation private, so do this get at its option setting…

---

<div class="post-metadata">

**Author:** ![Doug](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/doug/32/7679_2.png) [@Doug](https://forums.swift.org/u/Doug)\
**Post date:** [February 18, 2023, 8:34am UTC](https://forums.swift.org/t/relative-date-style-of-date-formatstyle-datestyle/63091/6 "2023-02-18T08:34:10Z")

</div>

Thanks both. That looks interesting but a bit more involved than I had hoped. Will probably stick the the formatter for now.
