Skip to main content

XSD Date and Time

Date and time data types are used to represent date and time in the XML documents.

Date Data Type​

The date data type (<xs:date>) is used to specify a date.

The date is specified in the following format YYYY-MM-DD, where:

  • YYYY indicates the year
  • MM indicates the month
  • DD indicates the day
note

All components are mandatory!

Let's see an example:


<xs:element name="mydate" type="xs:date"/>

and an element in your XML document look like this:


<mydate>1999-06-14</mydate>

Time Zones​

To specify a time zone, you can either enter a date in UTC time by adding a Z behind the date


<mydate>1999-06-14Z</mydate>

or you can specify an offset from the UTC time by adding a positive or negative time behind the date


<mydate>2002-09-24-06:00</mydate>
<mydate>2002-09-24+06:00</mydate>

Time Data Type​

The time data type (<xs:dataType>) is used to specify a time.

The time is specified in the following form hh:mm:ss where:

  • hh indicates the hour
  • mm indicates the minute
  • ss indicates the second
note

All components are mandatory!

Let's see an example:


<xs:element name="mytime" type="xs:time"/>

and an element in your XML document look like this:


<mytime>15:00:00</mytime>
<mytime>15:28:10.5</mytime>

Time Zones​

To specify a time zone, you can either enter a time in UTC time by adding a Z behind the time:


<mytime>15:28:10Z</mytime>

or you can specify an offset from the UTC time by adding a positive or negative time behind the time:


<mytime>15:28:10-06:00</mytime>
<mytime>15:28:10+06:00</mytime>

DateTime Data Type​

The dateTime data type (<xs:dateTime>) is used to specify a date and a time.

The dateTime is specified in the following form YYYY-MM-DDThh:mm:ss where:

  • YYYY indicates the year
  • MM indicates the month
  • DD indicates the day
  • T indicates the start of the required time section
  • hh indicates the hour
  • mm indicates the minute
  • ss indicates the second
note

All components are mandatory!

Let's see an example:


<xs:element name="mydatetime" type="xs:dateTime"/>

and an element in your XML document look like this:


<mydatetime>1999-06-14T15:50:00</mydatetime>
<mydatetime>1999-06-14T15.50.00</mydatetime>

Time Zones​

To specify a time zone, you can either enter a dateTime in UTC time by adding a Z behind the time:


<mydatetime>1999-06-14T15:50:00Z</mydatetime>

or you can specify an offset from the UTC time by adding a positive or negative time behind the time:


<mydatetime>1999-06-14T15:50:00-06:00</mydatetime>
<mydatetime>1999-06-14T15:50:00+06:00</mydatetime>

Duration Data Type​

The duration data type (<xs:duration>) is used to represent time interval in PnYnMnDTnHnMnS format. Each component is optional except P.

  • P represents start of date section
  • nY represents year
  • nM represents month
  • nD represents day
  • T represents start of time section
  • nH represents hours
  • nM represents minutes
  • nS represents seconds
note

'T' is required if you are going to specify hours, minutes, or seconds.

The duration declaration is as follows:


<xs:element name="period" type="xs:duration"/>

Example of a period of 5 years:


<period>P5Y</period>

Example of a period of five years, two months, and 10 days:


<period>P5Y2M10D</period>

Example of a period of 15 hours:


<period>P5Y2M10D</period>

Negative Duration​

To specify a negative duration, enter a minus sign before the P.

Example of a period of minus 15 days:


<period>-P15D</period>

Date and Time Data Types​

The following is a list of commonly used date data types.

NameDescription
dateDefines a date value
dateTimeDefines a date and time value
durationDefines a time interval
gDayDefines a part of a date - the day (DD)
gMonthDefines a part of a date - the month (MM)
gMonthDayDefines a part of a date - the month and day (MM-DD)
gYearDefines a part of a date - the year (YYYY)
gYearMonthDefines a part of a date - the year and month (YYYY-MM)
timeDefines a time value

Restrictions on Date Data Types​

Restrictions that can be used with Date data types:

  • enumeration
  • maxExclusive
  • maxInclusive
  • minExclusive
  • minInclusive
  • pattern
  • whiteSpace

See XSD Restrictions chapter to learn more.