What does a single quote inside a C# date time format mean?
It's a literal string delimiter.
Anything between the single quotes is interpreted as a literal character, and will not be interpreted as a custom format string.
They are equivalent to escaping using a backslash: dd'T'HH
is the same as dd\THH
.
In your string yyyy'-'MM'-'dd'T'HH':'mm':'ss
, the quotes are unnecessary around the -
and T
, as those are not custom format strings, and so will always be interpreted as string literals. However :
is a format specifier, which evaluates to a time separator that's suitable for the current culture. Quoting it as ':'
means that the literal character :
will always be used, regardless of the current culture.
It's useful to know that this is the same format used by the Windows GetDateFormat function. You use apostrophe's to indicate some literal you want inserted into the resulting string.
More documentation of the string formatting codes can be found on:
- Day, Month, Year, and Era Format Pictures
In your string it is indiating that it literally wants to add hyphens, colons, and the T characters:
yyyy
'-'
MM'-'
dd'T'
HH':'
mm':'
ss
In general you don't want to construct dates/times with literally a hyphens (-
) or colons (:
), or even slashes (/
). Because those are wrong for cultures that don't use slashes and colons to construct dates/times:
- 10/22/2019 10∶19∶54 ᴀᴍ
- 10:19:54
- 2019.10.22 10:19:54
- 2019-10-22 10.19.54
- 2019-10-22 10:19:54
- 2019-10-22 10:19:54 AM
- 2019-10-22 ཆུ་ཚོད་10:19:54 སྔ་ཆ་
- 2019-10-22 오전 10:19:54
- 2019 10:19:54
- 10:19:54
- 22.10.19 10:19:54
- 22.10.19 ý. 10:19:54
- 22.10.2019 10.19.54
- 22.10.2019 10:19:54
- 22.10.2019 10:19:54 p.d.
- 22.10.2019 г. 10:19:54
- 22.10.2019. 10:19:54
- 22-10-19 10.19.54
- 22-10-19 10.19.54 AM
- 22-10-19 10:19:54
- 22-10-19 ਸਵੇਰ 10:19:54
- 22-10-2019 10:19:54
- 22-10-2019 ৰাতিপু 10:19:54
- 22-окт. 19 10:19:54
- 23/02/41 10:19:54 ص
- 30/07/1398 10:19:54 ق.ظ
What you would want to do, if you want to indicate:
- "here is where you should put the date separator"
- "here is where you should put the time separator"
Is use the special:
- date separator replacement character (/)
- time separator replacement character (:)
And construct a format such as:
dd/yyyy/MM ss:HH:mm
I don't know what business need one might have to show a date as:
- 22/2019/10 37:10:24
- 22-2019-10 37∶10∶24
- 22//2019//10 37::10::24
- 10 37∶10∶24
But this way the localizer will insert the culture's correct date and time separators.
Whereas if you literally asked for /
, -
, or :
:
- dd
'/'
yyyy'/'
MM ss':'
HH':'
mm
You will literally get:
- 22/2019/10 37:10:24
rather than:
- 22-2019-10 37∶10∶24