-2

The datetime type in sitecore field I retrieved through query is "20161114T000000Z" string.

Question:

  1. What is T and Z?

  2. How should parse it to DateTime object?

I read some related questions and found DateTime.TryParseExact() in this thread could solve the problem, but still I don't know what is T and Z mean.

Please provide some sample code.

Community
  • 1
  • 1
Nicolas S.Xu
  • 13,794
  • 31
  • 84
  • 129
  • T is the separator between Date and Time. Z is for zulu time or UTC. – Josh J Apr 11 '17 at 21:27
  • "Please provide some sample code". I think you might be confused on what the purpose of Stack Overflow is. We're not here to write code for you. Please read [ask]. For the T and Z, read https://en.wikipedia.org/wiki/ISO_8601 – Heretic Monkey Apr 11 '17 at 21:29
  • @JoshJ Any suggestion about the format string for parsing it? e.g. "yyyy-MM-dd HH:mm:ss,fff" – Nicolas S.Xu Apr 11 '17 at 21:30
  • 4
    Not beating up anyone. Just informing you of the rules of the site. In fact, if anyone should be offended, it should be me, since it appears you feel your time is more valuable than mine. – Heretic Monkey Apr 11 '17 at 21:36
  • Welcome Nicolas. Mike's right, but I answered anyway, since I was bored. But be a good community member and please read this: [Ask] On most days your question would have been closed before I could answer. – John Wu Apr 12 '17 at 00:39
  • I'm going to side with Mike on this one. I gave you the answer to your first part of the question and it should be enough for you to figure out the second part. Show us code that you are trying and we can help more. No one benefits from a copy and paste code service, but if you learn some new tricks you may be able to teach them to someone else later on. Good luck! – Josh J Apr 12 '17 at 00:40
  • 10
    Answered here: http://sitecore.stackexchange.com/questions/4833/what-is-the-datetime-format-used-by-sitecores-date-datetime-field-types-and-h – Mark Cassidy Apr 12 '17 at 06:40

1 Answers1

3

The T is just a delimiter. The Z tells us the time is in UTC. What we're looking at is a variant of ISO-8601 Combined Date and Time format.

To parse this string you could use

using System.Globalization;

var s = "20161114T000000Z";
var d = DateTime.ParseExact(s, "yyyyMMdd'T'HHmmss'Z'", CultureInfo.InvariantCulture);

Microsoft' latest guidance is to prefer the DateTimeOffset structure for unambiguously storing a point in time, so perhaps you should use this instead:

var s = "20161114T000000Z";
var d = DateTimeOffset.ParseExact(s, "yyyyMMdd'T'HHmmss'Z'", CultureInfo.InvariantCulture);
John Wu
  • 50,556
  • 8
  • 44
  • 80
  • Best practice is actually to use the built in Sitecore methods e.g. Sitecore.DateUtil.IsoDateToDateTime([CanBeNull] string isoDate, DateTime defaultValue, bool treatLegacyDatesAsUtc) – geedubb Oct 22 '18 at 07:07
  • 1
    @geedubb Do you have a citation for that? I am curious why it is better. Will it work with the OP's example (which is not quite exactly a standard ISO date/time string)? – John Wu Oct 22 '18 at 07:11
  • var isoDate = "20161114T000000Z"; Response.Write( Sitecore.DateUtil.IsoDateToDateTime(isoDate, DateTime.Now, true) ); -> will print 14/11/2016 00:00:00. https://doc.sitecore.net/sitecore_experience_platform/setting_up_and_maintaining/utc/datetime/datetime_best_practices – geedubb Oct 22 '18 at 11:16