0

I have built a dynamic form system, that accepts types of textfields, dropdowns and datetimes.

The form answers are also dynamic, everything is stored as text, or a text version of the id to the dropdown answer.

For datetimes, I want to know what the best format to serialise a c# datetime to text is, purely to use for seralisation. I already have to and from Unix tics extension working, which I could use for this as well as my javascript calls. But I feel there is a better option.

Obviously DateTime.Parse will be used on the way back, but what to use on the way out?

1
  • Do you need only dates or also times? Do you need to execute queries on these fields or not? Commented Jul 7, 2012 at 14:22

2 Answers 2

4

You'd just use ToString with an appropriate standard or custom date/time format string.

However, you need to be very careful about this:

  • I'd suggest using CultureInfo.InvariantCulture to perform the conversion (and likewise when parsing) to avoid culture-specific values
  • You need to consider whether to also serialize the Kind of the DateTime. Are these always local, universal or unspecified DateTime values? Should you actually use DateTimeOffset and serialize the offset too?
  • What precision do you need?
  • What else will be reading this data? A "standard" format such ISO-8601 might be a good idea, potentially extended to maintain the same precision as DateTime itself. (The standard o or O format string will produce ISO-8601 strings.)
Sign up to request clarification or add additional context in comments.

Comments

0

For round-tripping DateTime values the obvious solution is to use the "o" format specifier.

Unless you have constraints on the length of the string, and are prepared to sacrifice either precision, the Kind property (UTC or Local), or both. In this case use a custom format string that meets your needs.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.