# Question about round-tripping with Date.ParseStrategy and Date.FormatStyle

**URL:** <https://forums.swift.org/t/question-about-round-tripping-with-date-parsestrategy-and-date-formatstyle/70590>\
**Category:** Foundation\
**Tags:** foundation, formatter, formatstyle\
**Created:** [March 12, 2024, 2:43am UTC](https://forums.swift.org/t/question-about-round-tripping-with-date-parsestrategy-and-date-formatstyle/70590 "2024-03-12T02:43:32Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![James\_Dempsey](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/james_dempsey/32/14336_2.png) [@James\_Dempsey](https://forums.swift.org/u/James_Dempsey)\
**Post date:** [March 12, 2024, 2:43am UTC](https://forums.swift.org/t/question-about-round-tripping-with-date-parsestrategy-and-date-formatstyle/70590/1 "2024-03-12T02:43:32Z")

</div>

I am working with a text file format that I do not have control over.

Dates are represented with strings like "2014\_03\_11" using an underscore to separate them rather than a space or a hyphen.

I am able to create a `Date.ParseStrategy` that parses the strings into dates:

```swift
let strategy = Date.ParseStrategy(
    format: "\(year: .defaultDigits)_\(month: .twoDigits)_\(day: .twoDigits)",
    timeZone: TimeZone.current)

```

I cannot seem to find a way to round-trip a date back to a string using an underscore as the separator using a ` Date.FormatStyle`.

I seem to be able to specify each date component, but not how they are separated.

Is there a way to do this using `Date.FormatStyle` or some other piece of the most recent APIs? Or do I need to go back to using DateFormatter for this?

Thanks for any pointers or assistance.

---

<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:** [March 12, 2024, 3:08am UTC](https://forums.swift.org/t/question-about-round-tripping-with-date-parsestrategy-and-date-formatstyle/70590/2 "2024-03-12T03:08:38Z")

</div>

```swift
print(Date().formatted(Date.VerbatimFormatStyle(
    format: Date.FormatString("\(year: .padded(4))_\(month: .twoDigits)_\(day: .twoDigits)"),
    timeZone: .current,
    calendar: .current
))) // 2024_03_12

```

---

<div class="post-metadata">

**Author:** ![James\_Dempsey](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/james_dempsey/32/14336_2.png) [@James\_Dempsey](https://forums.swift.org/u/James_Dempsey)\
**Post date:** [March 12, 2024, 3:14am UTC](https://forums.swift.org/t/question-about-round-tripping-with-date-parsestrategy-and-date-formatstyle/70590/3 "2024-03-12T03:14:03Z")

</div>

Thank you @tera!

I never would have guessed the name 'VerbatimFormatStyle" in order to search for it.

---

<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:** [March 12, 2024, 3:18am UTC](https://forums.swift.org/t/question-about-round-tripping-with-date-parsestrategy-and-date-formatstyle/70590/4 "2024-03-12T03:18:16Z")

</div>

Yeah, autocomplete doesn't quite help here either, only when you know the answer partially already.

Beware of the calendar / timezone and the missing (defaulted) locale parameters. e.g. if the calendar is set to Chinese things won't be formatted the way you expect.

---

<div class="post-metadata">

**Author:** ![James\_Dempsey](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/james_dempsey/32/14336_2.png) [@James\_Dempsey](https://forums.swift.org/u/James_Dempsey)\
**Post date:** [March 12, 2024, 3:19am UTC](https://forums.swift.org/t/question-about-round-tripping-with-date-parsestrategy-and-date-formatstyle/70590/5 "2024-03-12T03:19:28Z")

</div>

Thank you, yes, I'll be setting the calendar, time zone, and locale explicitly.
