stevenvh
stevenvh

Reputation: 3139

Date elements in XML

I guess there are two major approaches to date representations in XML:

<date>1984-10-27</date>

and

<date>
  <year>1984</year>
  <month>10</month>
  <day>27</day>
</date>

Personally I would go for the former. It's more compact and at the same time more readable. The split-up of the second form seems overkill to me; e.g. most of the time the month in itself will have no information value.

Also you can't validate an element like <day> independently: you can define a range of [1..31], but 31 isn't acceptable if <month> is 2.

On the other hand the first form could cause confusion, especially if you use (FFF) MM-DD-YYYY or DD-MM-YYYY. I always use ISO 8601 format (YYYY-MM-DD), as it avoids this confusion.

Which XML code do you prefer?

Upvotes: 2

Views: 1005

Answers (4)

Robert Rossney
Robert Rossney

Reputation: 96702

While I agree with lucas-insasho that seconds-since-epoch is generally the easiest date/time representation to work with, it's not generally the easiest one to work with in XML. It can't be usefully processed by XSLT 1.0, nor can it be validated using XML Schema.

You can of course represent data in XML however you want. But really the only reason to use XML in the first place (instead of a more lightweight serialization format like JSON or YAML) is to reap those benefits that standardization has to offer.

Upvotes: 2

Paxic
Paxic

Reputation: 1760

I vote for broken up. Whats wrong with the first you ask?

  • is that day/month or month/day ...errors are likely both human and machine
  • needs to be tokenized to get out the date information
  • you are storing formatting(separator) characters

Upvotes: 0

Marc Gravell
Marc Gravell

Reputation: 1062745

The standard for dates/times in xml is much more formal than that; most xml parsers will respect (for example):

  • 2009-02-15
  • 2009-02-15T10:20:34.0000000
  • 2009-02-15T10:20:34.0000000+00:00

This is always yyyy-MM-dd; the time portion is optional, as is the UTC offset.

(edit: lucas-insasho correctly cited the spec)

Upvotes: 3

pawstrong
pawstrong

Reputation: 954

Practical experience has taught me that the easiest date values to work with is seconds-since-epoch. This value is portable across all languages and is timezone insensitive (your local date libraries will handle it).

For better or worse, XML tends to prefer more verbose and human readable values. If you want to use a human readable date value, I would recommend using the standard time format described by ISO 8601: http://www.w3.org/TR/NOTE-datetime. This is an unambiguous and internationally understood format. Parsers exist for various languages and it is generally the best textual representation of date values.

Upvotes: 6

Related Questions