14

When I turned on "Raw values" in Sitecore to examine a Date (or DateTime) field, I see a string in the form

20170310T130700Z

Looking at the regular .Net DateTime type, I don't see a standard format option which reproduces this format. See here https://msdn.microsoft.com/en-us/library/zdtaw1bw(v=vs.110).aspx

What is an easy way to generate this format from a regular DateTime object and what is the name of this format used by Sitecore?

13

Scroll to the bottom for the updated answer


This format (with T in the middle and ending with Z) is called by Sitecore ShortIsoDateTime. I don't know if that is official name of it or not.

You can get it using Sitecore.DateUtil.GetShortIsoDateTime(dateTime) method.

There are more formats which are used by Sitecore.

Sitecore.DateUtil class is a really good place to learn more about them.

And there is a guide on Sitecore Docs: Date/time best practices


EDIT:

Z character is added only if

if (datetime.Kind == DateTimeKind.Utc)
    str += "Z";

EDIT 2:

Just run a test:

DateTime dateTime = DateTime.Now;
string string1 = Sitecore.DateUtil.GetShortIsoDateTime(dateTime);
// string1 is 20170310155908

dateTime = Sitecore.Common.DateTimeExtensions.SpecifyKind(dateTime, DateTimeKind.Utc);
string string2 = Sitecore.DateUtil.GetShortIsoDateTime(dateTime);
// string2 is 20170310155908Z

EDIT 3:

You are right. I was wrong since the beginning. The correct answer is: This format is called by Sitecore IsoDate:

    DateTime dateTime = DateTime.Now;
    string dateTimeString1 = Sitecore.DateUtil.ToIsoDate(dateTime);
    // dateTimeString1 == "20170310T162500"

    dateTime = Sitecore.Common.DateTimeExtensions.SpecifyKind(dateTime, DateTimeKind.Utc);
    string dateTimeString2 = Sitecore.DateUtil.ToIsoDate(dateTime);
    // dateTimeString2 == "20170310T162500Z"
  • That method actually returns in yyyyMMddhhmmss format for me (without the T and the Z) – Matthew Dresser Mar 10 '17 at 13:35
  • OK I'm confused now. In linqpad it returns the yyyyMMddhhmmssZ format whereas in a unit test I get yyyyMMddhhmmss... – Matthew Dresser Mar 10 '17 at 13:38
  • 1
    I think it depends on whether yourDateTime.Kind is DateTimeKind.Unspecified, DateTimeKind.Utc or DateTimeKind.Local. – Marek Musielak Mar 10 '17 at 13:45
  • Answer edited. "Z" is added only for DateTimeKind.Utc dates – Marek Musielak Mar 10 '17 at 13:50
  • 2
    The correct date format is ISO 8601 btw: en.wikipedia.org/wiki/ISO_8601 – jammykam Mar 10 '17 at 16:36
0

From a different side (.net / c#), you can always use custom formatter to format the date into any desired format.

For example, to convert the current datetime to the format that Sitecore uses, you could do the folllowing in c#:

Console.WriteLine(DateTime.Now.ToString("yyyyMMddTHHmmssZ"));

which produces the following output:

enter image description here

More on this can be found at https://msdn.microsoft.com/en-us/library/8kb3ddd4(v=vs.110).aspx

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.