55

We have e.g. this constant in C#:

System.Globalization.DateTimeFormatInfo.InvariantInfo.SortableDateTimePattern

It has this value:

"yyyy'-'MM'-'dd'T'HH':'mm':'ss"

I do use it in the ParseExact function and then yyyy-MM-ddTHH:mm:ss works as well.

I cannot figure out what the single quotes are for.

72

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.

2

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:

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 ᴀᴍ
        1. 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
      1. 2019 10:19:54
        1. 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
      1. 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
  • I don't think .NET's date and time format strings are identical to GetDateFormat. Similar perhaps, but for example the meaning of yyyyy is different. Given that this question is getting a remarkable amount of attention, and it's about C#/, I suggest correcting this statement and linking to the .NET docs instead. – canton7 Oct 22 at 21:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.