DateFormat

Hi, Is the date Format "dd.mm.yyyy" available?

Answered by DTS Engineer in 683794022
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 use NSISO8601DateFormatter.

  • 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 call setLocalizedDateFormatFromTemplate(_:).

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

Accepted Answer

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

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 use NSISO8601DateFormatter.

  • 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 call setLocalizedDateFormatFromTemplate(_:).

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

DateFormat
 
 
Q