A date-only string like "2018-07-26" has no time zone, so decide on one when you parse it. Parse it as midnight UTC with a DateFormatter set to en_US_POSIX, UTC and yyyy-MM-dd, show it with a Date.FormatStyle that uses the same UTC zone, and check the result by formatting it back. That keeps the day from moving when the user's device is in Jakarta or Los Angeles, and it rejects dates like February 30.
SWIFT TIPS · FOUNDATION
First posted on July 30, 2018 for Swift 4, as "Swift 4 “Change String to Date without time”". Rewritten on November 17, 2026. All code and output below was run on a Mac with Swift 6.2.3 (Xcode 26.2) on macOS 26.0.1, with the time zone set to Asia/Jakarta and to America/Los_Angeles. It wasn't run on iOS.
In 2018 I had a date string in the form "2018-07-26" and wanted to show it as "26 July 2018". The post parsed the string with a DateFormatter, stripped the time with Calendar, and formatted it again, tested in a Playground.
Running it again now shows the catch. The result depends on the time zone of the machine that runs it. In a Playground in the same zone as the date, the day comes out right. On a phone set to another zone, the "date without time" can quietly become a different day.
What the 2018 code actually printed
I ran the parsing part of the original again, with the Mac's time zone set to Jakarta:
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-M-d"
dateFormatter.locale = Locale(identifier: "en_US_POSIX")
let date = dateFormatter.date(from: "2018-07-26")!
print(date)
2018-07-25 17:00:00 +0000
With the time zone set to Los Angeles, the same lines printed 2018-07-26 07:00:00 +0000.
Neither is a bug in Foundation. A Date is a moment in time, not a calendar day. A formatter with no time zone set uses the device's zone, so it turns "July 26" into midnight in Jakarta, which is 17:00 on July 25 in UTC, and print always shows UTC. That's why so many people think their date is "one day off". Removing the time with Calendar.current afterwards doesn't help, because it uses the same device zone.
The real problem shows up when the parsing and the display happen in different zones, for example parsing on the server and displaying on the phone, or a user who travels. Then you get the previous or the next day.
Parse a day as a day
import Foundation
/// Parses an API day string such as "2018-07-26" as midnight UTC.
/// Returns nil for anything that isn't a real calendar day.
func apiDay(_ string: String) -> Date? {
let f = DateFormatter()
f.locale = Locale(identifier: "en_US_POSIX")
f.timeZone = TimeZone(identifier: "UTC")
f.dateFormat = "yyyy-MM-dd"
guard let date = f.date(from: string), f.string(from: date) == string else { return nil }
return date
}
/// Shows a day to a person, in their language, without shifting it.
func displayDay(_ date: Date, locale: Locale = .current) -> String {
date.formatted(Date.FormatStyle(date: .long, time: .omitted, locale: locale,
timeZone: TimeZone(identifier: "UTC")!))
}
for s in ["2018-07-26", "2018-7-26", "2018-02-30", "2018-13-01", "26/07/2018"] {
if let d = apiDay(s) {
print(s.debugDescription, "->", d, "->", displayDay(d, locale: Locale(identifier: "id_ID")), "/", displayDay(d, locale: Locale(identifier: "en_US")))
} else {
print(s.debugDescription, "-> nil")
}
}
Run with the Mac set to Jakarta, and again set to Los Angeles, the output was the same both times:
"2018-07-26" -> 2018-07-26 00:00:00 +0000 -> 26 Juli 2018 / July 26, 2018
"2018-7-26" -> nil
"2018-02-30" -> nil
"2018-13-01" -> nil
"26/07/2018" -> nil
Three choices make this stable:
- A fixed time zone. The parser uses UTC no matter where the device is, so "2018-07-26" is always the same moment. The display style uses UTC too, so it turns that moment back into July 26. Any fixed zone works, as long as parsing and display use the same one.
en_US_POSIX. Apple'sDateFormatterdocumentation says that for most fixed formats you should set the locale toen_US_POSIXand the time zone to UTC, which is exactly this setup. Its Q&A on internet dates explains why: that locale gives the same result whatever the user's own settings are.- A round trip. After parsing, the code formats the date again and compares the result with the input. That's what rejects "2018-02-30", as the next section shows.
The display part uses Date.FormatStyle, which came in iOS 15. With the date style .long, the same day reads "26 Juli 2018" for Indonesian and "July 26, 2018" for US English. There's no format string to translate.
The test that surprised me
While checking this, I tried a few invalid strings against different formatter setups. A formatter with only locale, UTC and format, the one above, returned nil for "2018-02-30". But adding one more line that looks harmless, calendar = Calendar(identifier: .gregorian), made the same formatter accept "2018-02-30" and return March 2. isLenient was false in both cases. Date.ISO8601FormatStyle and a Date.ParseStrategy with a two-digit day did the same rollover to March 2.
That was on macOS 26.0.1. I didn't test whether iOS behaves the same, so I don't rely on the parser to reject bad dates at all. The round trip in apiDay catches it whichever way the parser behaves: "2018-03-02" doesn't equal "2018-02-30", so the function returns nil.
The round trip has one side effect. It also rejects "2018-7-26", because the formatter writes the month with two digits. If your API sends single-digit months, the 2018 format yyyy-M-d parses both, and you can compare the year, month and day numbers instead of the strings.
Dropping the time from a Date you already have
Sometimes you start with a full timestamp and want "today" for grouping or comparison. Use Calendar.startOfDay(for:) with a calendar set to the zone you care about. In my run, the moment 01:30 UTC on November 17, 2026 gave a start of day of 2026-11-16 17:00:00 +0000 in a Jakarta calendar, which is midnight on November 17 in Jakarta. For "same day" checks, isDate(_:inSameDayAs:) on that calendar reads better than comparing dates yourself.
A quick check for your project
- Search for
DateFormatter(). For every formatter that parses API data, check that it sets bothlocaleandtimeZone. - Change your simulator's or Mac's time zone to one on the other side of UTC from yours, such as Los Angeles if you're in Asia. Open every screen that shows a date-only value and see whether any day changes.
- Feed your parser "2018-02-30". If you get a date back, add a round-trip check.
If this saved you a debugging session, you can buy me a coffee.
Keep reading on JR Devhub
Comments