XSD Date and Time Data Types

For values containing date and time, the date and time data types are used.

Date Data Type:

To specify a date, the date data type is used. It is specified in the “YYYY-MM-DD” format and where all components are required. Here,

  • YYYY: Used to indicate the year.
  • MM: Used to indicate the month.
  • DD: Used to indicate the day.

Example: Date declaration in a schema:

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

An element in a document:

<birthday>1995-11-16</birthday>

Time Zones:

To specify a time zone:

Enter a date in UTC time by adding a “Z” behind the date:

Example:

<birthday>1995-11-16Z</birthday>

OR

Specify an offset from the UTC time by adding a positive or negative time behind the date:

Example 1:

<birthday>1995-11-16-09:00</birthday>

Example 2:

<birthday>1995-11-16+09:00</birthday>

Time Data Type:

To specify a time, the time data type is used. It is specified in the “hh:mm:ss” format and where all the components are required. Here,

  • hh: Used to indicate the hour.
  • mm: Used to indicate the minute.
  • ss: Used to indicate the second.

Example: Time declaration in a schema:

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

An element in a document:

<birthtime>21:00:00</birthtime>

Or:

<birthtime>21:30:01.7</birthtime>

Time Zones:

To specify a time zone:

Enter a time in UTC time by adding a “Z” behind the time:

Example:

<birthtime>21:30:10Z</birthtime>

OR

Specify an offset from the UTC time by adding a positive or negative time behind the time:

Example 1:

<birthtime>21:30:10-03:00</birthtime>

Example 2:

<birthtime>21:30:10+03:00</birthtime>

DateTime Data Type:

To specify a date and a time, the dateTime data type is used. It is specified in the “YYYY-MM-DDThh:mm:ss” format and where all the components are required. Here,

  • YYYY: Used to indicate the year.
  • MM: Used to indicate the month.
  • DD: Used to indicate the day.
  • T: Used to indicate the start of the required time section.
  • hh: Used to indicate the hour.
  • mm: Used to indicate the minute.
  • ss: Used to indicate the second.

Example: DateTime declaration in a schema:

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

An element in a document:

<start>2020-08-08T08:00:00</start>

Or:

<start>2020-08-08T08:30:11.5</start>

Time Zones:

To specify a time zone:

Enter a dateTime in UTC time by adding a “Z” behind the time:

Example:

<start>2020-08-08T08:30:11Z</start>

OR

Specify an offset from the UTC time by adding a positive or negative time behind the time:

Example 1:

<start>2020-08-08T08:30:11-03:00</start>

Example 2:

<start>2020-08-08T08:30:11+03:00</start>

Duration Data Type:

To specify a time interval, the duration data type is used. It is specified in the “PnYnMnDTnHnMnS” format. Here,

  • P: Used to indicate the period. It is a required component.
  • nY: Used to indicate the number of years.
  • nM: Used to indicate the number of months.
  • nD: Used to indicate the number of days.
  • T: Used to indicate the start of a time section. It is a required component when we are specifying hours, minutes, or seconds.
  • nH: Used to indicate the number of hours.
  • nM: Used to indicate the number of minutes.
  • nS: Used to indicate the number of seconds.

Example: Duration declaration in a schema:

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

An element in a document:

<period>P2Y</period>

Explanation:

In the above example, we are indicating a period of two years.

Or:

<period>P2Y3M4D</period>

Explanation:

In the above example, we are indicating a period of two years, three months, and four days.

Or:

<period>P2Y3M4DT5H</period>

Explanation:

In the above example, we are indicating a period of two years, three months, four days and five hours.

Or:

<period>PT5H</period>

Explanation:

In the above example, we are indicating a period of five hours.

Negative Duration:

We can enter a minus sign before P to indicate a negative duration.

Example:

<period>-P4D</period>

Explanation:

In the above example, we are indicating a period of minus four days.

Date and Time Data Types:

Name Uses
date Used to define a date value.
dateTime Used to define a date and time value.
duration Used to define a time interval.
gDay Used to define a part of a date – the day (DD).
gMonth Used to define a part of a date – the month (MM).
gMonthDay Used to define a part of a date – the month and day (MM-DD).
gYear Used to define a part of a date – the year (YYYY).
gYearMonth Used to define a part of a date – the year and month (YYYY-MM).
time Used to define a time value.

Restrictions on Date Data Types:

Restrictions used with Date data types:

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