As I said before here and here:
Dates have no format
A date is just a concept, an idea: it represents a specific point in the calendar.
The date of "1 March 2019" represents this: the specific point of the calendar, which corresponds to the 1st of March of the year 2019. To express this idea in text form, I can write it in different ways:
- 01/03/2019 (a common format in many countries, including Brazil)
- 3/1/2019 (American format, reversing day and month)
- 2019-03-01 (the format ISO 8601)
- 1st of March 2019 (in good Portuguese)
- March 1st, 2019 (in English)
- 2019 年 3 月 1 日 (in Japanese)
- and many others...
Note that each of the above formats is different, but all represent the same date (the same numerical values of the day, month and year).
That said, the string 2019-03-01
is just a text that represents the date in a specific format. When you turn this string into a DateTime
, is creating an object that contains the respective values (day 1, month 3, year 2019). But the date itself is not in a specific format.
When you print the date, of course, it has to be shown in some format. Methods like WriteLine
, on receiving a DateTime
, use a default format as per CurrentCulture
(see detailed explanation here).
If you want to use a specific format without relying on CurrentCulture
(or without needing to change it), use ToString(formato)
. Example:
d.ToString("yyyy-MM-dd") // retorna 2019-03-01
Remembering that this method returns a string: a representation of the date in a specific format, not the date (the DateTime
) in itself.
Great answer! I was going to improve mine, but this one is perfect.
– Jéf Bueno