Hi, Is the date Format "dd.mm.yyyy" available?
Apple Recommended
let dateFormatter = DateFormatter()
dateFormatter.calendar = Calendar.current
dateFormatter.dateFormat = "dd.MM.yyyy"
Do not write code like this. The problem here is that you’re using the current calendar, which may not be Gregorian, and thus you may get results that are wildly wrong. See QA1480 NSDateFormatter and Internet Dates for a concrete example of how this can end badly.
You have two choices here:
-
If you’re working with a fixed-format date — for example, parsing a date that’s returned from a server — either pin the locale to
en_US_POSIX
(see QA1480) or useNSISO8601DateFormatter
. -
If you’re working with a localised date — one that you display to the user — either set the
dateStyle
to one of the standard styles or callsetLocalizedDateFormatFromTemplate(_:)
.
Share and Enjoy
—
Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"
Replies
Up to you to define:
let dateFormatter = DateFormatter()
dateFormatter.calendar = Calendar.current
dateFormatter.dateFormat = "dd.MM.yyyy"
let dateText = dateFormatter.string(from: Date())
print(dateText)
Gives:
04.08.2021
Note: take care: mm is for minutes, MM for month
-
Hi Claude31, Todays date is 04.08.2021. But the date I got in console is "04.48.2021"
-
That's because you wrote mm and not MM. dateFormatter.dateFormat = "dd.MM.yyyy" What you get are the minutes. You'd better read the answers you get. Note: take care: mm is for minutes, MM for month
let dateFormatter = DateFormatter()
dateFormatter.calendar = Calendar.current
dateFormatter.dateFormat = "dd.MM.yyyy"
Do not write code like this. The problem here is that you’re using the current calendar, which may not be Gregorian, and thus you may get results that are wildly wrong. See QA1480 NSDateFormatter and Internet Dates for a concrete example of how this can end badly.
You have two choices here:
-
If you’re working with a fixed-format date — for example, parsing a date that’s returned from a server — either pin the locale to
en_US_POSIX
(see QA1480) or useNSISO8601DateFormatter
. -
If you’re working with a localised date — one that you display to the user — either set the
dateStyle
to one of the standard styles or callsetLocalizedDateFormatFromTemplate(_:)
.
Share and Enjoy
—
Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"