Skip to content

Commit 9b86c78

Browse files
committed
Clarify Foundation date formatter
1 parent a004215 commit 9b86c78

File tree

1 file changed

+13
-3
lines changed

1 file changed

+13
-3
lines changed

Apple/Foundation.md

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -153,13 +153,23 @@ DateFormatter
153153
For example:
154154

155155
```swift
156+
// Create
156157
let dateFormatter = DateFormatter()
157-
dateFormatter.dateFormat = "YYYY-MM-dd"
158-
dateFormatter.timeZone = TimeZone(abbreviation: "UTC")
158+
159+
// Date style
160+
dateFormatter.dateStyle = .medium
161+
162+
// Time style
163+
dateFormatter.timeStyle = .none
159164

160165
// String to date
161-
dateFormatter.date(from: "2000-01-01")
166+
dateFormatter.date(from: someString)
162167

163168
// Date to string
164169
dateFormatter.string(from: someDate)
170+
171+
// Override local values like for unit tests
172+
dateFormatter.locale = Locale(identifier: "en_US")
173+
dateFormatter.timeZone = TimeZone(abbreviation: "UTC")
174+
dateFormatter.dateFormat = "YYYY-MM-dd"
165175
```

0 commit comments

Comments
 (0)