9️⃣ 🔢 XSD Data Types
Estimated reading: 3 minutes 56 views

📅 XSD Date/Time – Define and Validate Temporal Values in XML

🧲 Introduction – Why Use XSD Date and Time Types?

Handling dates and times correctly is crucial in XML documents that record events, logs, timestamps, schedules, or deadlines. XSD (XML Schema Definition) provides built-in date and time data types to ensure that all time-related values are consistent, validated, and properly formatted.

🎯 In this guide, you’ll learn:

  • How to use xs:date, xs:time, xs:dateTime, and related types
  • Accepted ISO 8601 formats for these types
  • How to apply restrictions like min/max dates
  • Real-world examples for schedules, logs, and durations

📘 Common Date/Time Types in XSD

TypeDescriptionExample Value
xs:dateA calendar date2025-12-31
xs:timeA specific time14:30:00
xs:dateTimeFull date and time (with optional timezone)2025-12-31T14:30:00Z
xs:gYearA specific year2025
xs:gYearMonthA year and month2025-06
xs:durationA length of time (ISO 8601 format)P2Y6M5DT12H35M30S

🧾 Basic Examples

🔹 Date

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

✅ Valid XML:

<startDate>2025-08-15</startDate>

🔹 Time

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

✅ Valid XML:

<startTime>09:30:00</startTime>

🔹 DateTime

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

✅ Valid XML:

<createdAt>2025-08-15T09:30:00Z</createdAt> <!-- UTC time -->

⏳ Duration (Time Interval)

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

✅ Valid Values:

  • P1Y → 1 year
  • P3M → 3 months
  • P1DT12H → 1 day, 12 hours
  • P2Y6M5DT12H35M30S → 2 years, 6 months, 5 days, 12 hours, 35 minutes, 30 seconds

🛑 Restricting Date/Time Values

🔹 Example – Date Range

<xs:element name="birthDate">
  <xs:simpleType>
    <xs:restriction base="xs:date">
      <xs:minInclusive value="1900-01-01"/>
      <xs:maxInclusive value="2025-12-31"/>
    </xs:restriction>
  </xs:simpleType>
</xs:element>

✅ Accepts dates only in the 20th and early 21st century.


✅ Best Practices for XSD Date/Time

  • ✔️ Use xs:dateTime when both date and time are needed
  • ✔️ Use xs:duration to represent time intervals (like trials or subscriptions)
  • ✔️ Always follow ISO 8601 format (e.g., YYYY-MM-DD, HH:MM:SS)
  • ✔️ Use minInclusive and maxInclusive to restrict allowed values
  • ❌ Avoid custom string formatting—use proper XSD types instead
  • ❌ Don’t forget timezone notation if working with global systems (Z for UTC)

📌 Summary – Recap & Next Steps

XSD Date and Time types provide built-in support for validating and formatting temporal values. With proper types and restrictions, you can ensure consistent, machine-readable time data across your XML systems.

🔍 Key Takeaways:

  • Use xs:date, xs:time, xs:dateTime for timestamped values
  • Use xs:duration for time intervals (e.g. P1Y6M = 1 year 6 months)
  • Restrict ranges with minInclusive and maxInclusive

⚙️ Real-world relevance: Used in invoices, reports, events, reservations, system logs, and subscription plans.


❓ FAQs – XSD Date/Time

❓ What format does xs:date use?
✅ ISO 8601: YYYY-MM-DD (e.g., 2025-08-15)

❓ Can xs:dateTime include time zones?
✅ Yes. Use Z for UTC or +hh:mm/-hh:mm for offsets.

❓ Can I compare date ranges in XSD?
✅ Yes, using minInclusive and maxInclusive restrictions.

❓ Can I use a date pattern with xs:date?
❌ No. xs:pattern is not typically used with date types—use min/max for validation.

❓ Is xs:duration for repeating periods?
❌ No. It’s for describing one span of time, not recurring intervals.


Share Now :

Leave a Reply

Your email address will not be published. Required fields are marked *

Share

XSD Date/Time

Or Copy Link

CONTENTS
Scroll to Top