Date time operations is one of those topics that are ubiquitous in programming but do not receive a lot of attention. It is somehow expected that humans intuitvely understand time since we deal with it so often in everyday life. As a result, many developers make silly mistakes when working with date time. In this blog post, we will look at some of these mistakes specifically when parsing/formatting date time.

Sorted by frequency of occurrence from least common to most common (and subtle).

A: Month vs minute

Lowercase “mm” stands for a minute in an hour, but uppercase “MM” stands for the month in a year. It is easy to mix them up.

Examples:

parseDateTime('2015-05-21', 'yyyy-mm-dd')

parseDateTime('13:37', 'HH:MM')

B: Now wait just a second

Lowercase “s” is for seconds but uppercase “S” is for milliseconds which is used much less often.

Example:

parseDateTime('13:37:01', 'HH:mm:SS')

C: What day is it? Well it's the 42nd of February of course.

Lowercase “dd” stands for day of the month, but uppercase “DD” stands for day of the year. Although there are valid use-cases for the latter the lowercase version is used much more often.

Example:

parseDateTime('2020-02-11', 'yyyy-MM-DD')

D: It is 17 PM o'clock

Lowercase "a" is used to denote AM or PM. It should only be used in combination with lowercase "h" which is in the range 1-12. Using "a" in combination with the uppercase "H" makes little sense.

Example:

formatDateTime($DateTime, 'HH:mm a')

E: Fun with hours