Last active
September 16, 2022 04:28
-
-
Save shaps80/761ee3cbfc40e726ad2ec20c8c7bf0d8 to your computer and use it in GitHub Desktop.
DateFormatter with better header docs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Foundation | |
public extension DateFormatter { | |
/** | |
Makes a new DateFormatter with the specified format, calendar and locale. | |
Characters Example Description | |
--------------------------------------------------------------------------------------------------------------- | |
Year | |
--------------------------------------------------------------------------------------------------------------- | |
y 2008 Year, no padding. Best used for user-facing localized dates | |
yy 08 Year, two digits (padding with a zero if necessary) | |
yyyy 2008 Year, minimum of four digits (padding with zeros if necessary) | |
--------------------------------------------------------------------------------------------------------------- | |
Quarter | |
--------------------------------------------------------------------------------------------------------------- | |
Q 4 The quarter of the year. Use QQ if you want zero padding. | |
QQQ Q4 Quarter including "Q" | |
QQQQ 4th quarter Quarter spelled out | |
--------------------------------------------------------------------------------------------------------------- | |
Month | |
--------------------------------------------------------------------------------------------------------------- | |
M 12 The numeric month of the year. A single M will use '1' for January. | |
MM 12 The numeric month of the year. A double M will use '01' for January. | |
MMM Dec The shorthand name of the month | |
MMMM December Full name of the month | |
MMMMM D Narrow name of the month | |
--------------------------------------------------------------------------------------------------------------- | |
Day | |
--------------------------------------------------------------------------------------------------------------- | |
d 14 The day of the month. A single d will use 1 for January 1st. | |
dd 14 The day of the month. A double d will use 01 for January 1st. | |
F 3rd Tuesday in December The day of week in the month | |
E Tues The day of week in the month | |
EEEE Tuesday The full name of the day | |
EEEEE T The narrow day of week | |
--------------------------------------------------------------------------------------------------------------- | |
Hour | |
--------------------------------------------------------------------------------------------------------------- | |
h 4 The 12-hour hour. | |
hh 04 The 12-hour hour padding with a zero if there is only 1 digit | |
H 16 The 24-hour hour. | |
HH 16 The 24-hour hour padding with a zero if there is only 1 digit. | |
a PM AM / PM for 12-hour time formats | |
--------------------------------------------------------------------------------------------------------------- | |
Minute | |
--------------------------------------------------------------------------------------------------------------- | |
m 35 The minute, with no padding for zeroes. | |
mm 35 The minute with zero padding. | |
--------------------------------------------------------------------------------------------------------------- | |
Second | |
--------------------------------------------------------------------------------------------------------------- | |
s 8 The seconds, with no padding for zeroes. | |
ss 08 The seconds with zero padding. | |
SSS 1234 The milliseconds. | |
--------------------------------------------------------------------------------------------------------------- | |
Time Zone | |
--------------------------------------------------------------------------------------------------------------- | |
zzz CST The 3 letter name of the time zone. Falls back to GMT-08:00 (hour offset) | |
if the name is not known. | |
zzzz Central Standard Time The expanded time zone name, falls back to GMT-08:00 (hour offset) | |
if name is not known. | |
ZZZZ CST-06:00 Time zone with abbreviation and offset | |
Z -0600 RFC 822 GMT format. Can also match a literal Z for Zulu (UTC) time. | |
ZZZZZ -06:00 ISO 8601 time zone format | |
Examples: | |
EEEE, MMM d, yyyy Monday, Dec 10, 2018 | |
MM/dd/yyyy 12/10/2018 | |
MM-dd-yyyy HH:mm 12-10-2018 15:47 | |
MMM d, h:mm a Dec 10, 3:47 PM | |
MMMM yyyy December 2018 | |
MMM d, yyyy Dec 10, 2018 | |
E, d MMM yyyy HH:mm:ss Z Mon, 10 Dec 2018 15:47:27 +0000 | |
yyyy-MM-dd'T'HH:mm:ssZ 2018-12-10T15:47:27+0000 | |
dd.MM.yy 10.12.18 | |
HH:mm:ss.SSS 15:47:27.514 | |
- Parameters: | |
- format: The date format to use. See references above | |
- calendar: The calendar to use for interpreting dates | |
- locale: The locale to use for interpreting dates | |
- seealso: | |
[Inspired by NSDateFormatter.com](https://nsdateformatter.com) | |
*/ | |
static func with(format: String, calendar: Calendar = .current, locale: Locale = .current) -> DateFormatter { | |
let formatter = DateFormatter() | |
formatter.calendar = calendar | |
formatter.locale = locale | |
formatter.dateFormat = format | |
return formatter | |
} | |
} |
Example Usage:
let date = Date()
let formatter = DateFormatter.with(format: "MMM yyyy",
calendar: Calendar(identifier: .gregorian),
locale: Locale(identifier: "en_US_POSIX"))
formatter.string(from: date)
Outputs:
"Dec 2018"
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
FYI, the code is wrapped to a 120 column width.