Skip to main content
Login

Sign in to access your account

Date - National Design System

Shows, reads and writes dates in the two calendars of Saudi Arabia, Gregorian and Hijri (Umm al-Qura)

Overview

NDS works with dates in two ways. In markup, a <time> with data-calendar="hijri" shows its date in Hijri, and one with its own data-date-format shows it in that format. You write no script. In JavaScript, NDS.Date reads, writes and converts dates. It is in the main bundle, so every page has it, with no init call.

A <time data-date-today> shows today’s date, in the top bar or anywhere on a page.

Four attributes on <html> set the site’s dates: data-timezone sets its timezone, data-date-format its date format, data-calendar its calendar, and data-week-start the first day of its week.

Pick another component when:

Markup

Preview
<time datetime="2026-10-18">18/10/2026</time>

Today

The script writes the date. The icon and the Meta line are yours, as in the top bar.

<span class="nds-meta"> <span class="nds-meta-item"><i class="nds-icon nds-hgi-calendar-03" aria-hidden="true"></i><time data-date-today data-calendar="hijri" data-date-format="long" data-date-era></time></span> </span>

Site Setup

Set the site’s timezone and date format on <html>. Without data-timezone, today is the visitor’s day. Without data-date-format, the format is DD/MM/YYYY.

<html lang="ar" dir="rtl" data-timezone="Asia/Riyadh" data-date-format="DD/MM/YYYY">

Give one part of the page its own format with data-date-format on any element.

<section data-date-format="MM/DD/YYYY"> <!-- A table here sorts 12/31/2026 as 31 December --> </section>

Dates in JavaScript

Read a Hijri date from a field, and send it to the server as YYYY-MM-DD.

// '15/09/1447' → '2026-03-04'. null when the text is not a Hijri date: show the field's error. function hijriToIso(text) { const date = NDS.Date.parse(text, { calendar: 'hijri', format: 'DD/MM/YYYY' }); return date && NDS.Date.format(date, { format: 'YYYY-MM-DD' }); }

Show a date to people, convert it, or write the month’s name.

// In the page language. On an Arabic page: 'الخميس، 5 مارس 2026' NDS.Date.format(new Date(2026, 2, 5), { weekday: 'long', day: 'numeric', month: 'long', year: 'numeric' }); // Gregorian to Hijri: '16/09/1447' NDS.Date.convert('05/03/2026', { format: 'DD/MM/YYYY' }, { calendar: 'hijri', format: 'DD/MM/YYYY' }); // The month's name with MMMM. On an Arabic page: '16 رمضان 1447' NDS.Date.format(new Date(2026, 2, 5), { calendar: 'hijri', format: 'D MMMM YYYY' });

Behavior

Calendar Days

A date is a calendar day: a JavaScript Date at local midnight. parse returns one, and format reads one. No timezone moves it, so a day typed in Riyadh is the same day for a visitor in Tokyo. The site’s timezone changes only which day getToday() returns.

Nearest Format

data-date-format, data-calendar and data-week-start on an element set the format, the calendar and the week start of the dates inside it. The nearest one wins over the one on <html>. Date Picker, Sort and a <time> the script writes read them.

Markup Dates

The script reads the date from datetime, never from the text, and writes the text before the page shows, so the line does not shift. It writes a <time> with its own data-calendar or data-date-format, in the nearest calendar and format. Any other <time> already reads right, so it stays as written.

datetime holds YYYY-MM-DD, with or without a time. A time with a zone, such as Z or +03:00, counts in the site’s timezone. A value that is not a full date is left as written, and without JavaScript the text you wrote stays. With data-date-era, a Hijri date ends in «هـ» on an Arabic page and AH on an English one.

When a page changes lang on <html> in place, the script writes every markup date again in the new language.

Today

A <time data-date-today> holds today, in the site’s timezone. The script writes its datetime and its text before the page shows. It writes them again at the site’s midnight, and when the tab shows again. The script reads the nearest calendar and format, as for any markup date. While data-hidden hides the date, the script writes nothing. It writes the date when the width changes into a band where the date shows. See Hidden.

Built-in Features

  • Umm al-Qura Hijri

    calendar: 'hijri' is Umm al-Qura, the official calendar of Saudi Arabia, for the years 1300 to 1600 AH, computed in the browser with no network call. Outside them, the browser uses the civil Hijri calendar, which can differ by a day.

  • Exact Conversion

    A Hijri date converts through the browser's own calendar data, not a table in NDS. A test checks every day from 2018 to 2037, both ways, against the browser's calendar.

  • Invalid Days

    A day that does not exist returns null, such as 31 April, or day 30 of a 29-day Hijri month. It never rolls over into the next month.

  • Arabic Digits

    parse reads Arabic digits, such as ٠٣/٠٤/٢٠٢٦. It also removes the direction marks that the browser writes into Arabic dates. format writes Arabic digits with numerals: 'arab'.

  • Site Timezone

    getToday() returns today in the timezone that data-timezone names. An invalid name logs one warning, and the visitor's timezone applies.

  • Live Today

    One timer serves every Today date on the page.

Best Practices

  • Name the format when the text comes from a server or another site. 03/04/2026 is 3 April or 4 March, and parse never guesses.
  • Check the result of parse for null before you use it.
  • Send a date to a server as YYYY-MM-DD. Show the user any format or calendar.
  • Write a readable date as the text of a <time>, not an empty tag. It shows before the script runs, without JavaScript, and to search engines. A Today date is the exception: write it empty.
  • Write data-date-format="long" on a Today date that people read. A numeric date such as 21/05/1448 is harder to read at a glance.
  • Use getToday() for today’s date, not new Date(). new Date() is the visitor’s clock and ignores data-timezone.
  • Set data-timezone when today must be the same day for every visitor, such as a deadline in Riyadh.
  • Do not pass timeZone to format for a parsed date. It moves the day for a visitor in another timezone. Pass it only for a moment in time, such as new Date() in a clock.
  • For a Hijri month name, use MMMM, not month: 'long'. On Android, the browser can print a Gregorian month name for a Hijri month, and MMMM takes the name from the string pack.
  • Sort reads a date from the text in the nearest data-date-format and data-calendar. For text in another format, write the date as a Gregorian YYYY-MM-DD in data-sort-value on a table cell, or in data-sort-{key} on a list item.

API

Data Attributes

Attribute Element Effect
data-timezone <html> The site’s timezone, as an IANA name such as Asia/Riyadh. It sets today in getToday(), Date Picker, Today, Clock, and the date in an Export file name. Without it, the visitor’s timezone applies
data-date-format <html> The site’s date format, in the tokens below. parse and format use it when you name no format. The default is DD/MM/YYYY
data-date-format any element The date format of the content inside it. The nearest one wins over the one on <html>. A Date Picker without its own data-format, and Sort when it reads a date from text, use it
data-calendar <html>, or any element hijri or gregory, the calendar of the dates inside it. The nearest one wins. Date Picker, markup dates and Sort read it. The default is Gregorian
data-week-start <html>, or any element sun, mon, tue, wed, thu, fri or sat: the first day of the week inside it. The nearest one wins. The Date Picker’s grid starts on it. The default is Sunday, and any other value warns in the console and uses Sunday
data-calendar a <time> with datetime On the <time> itself it opts the date in: hijri shows it in Hijri, gregory in Gregorian, and an empty value takes the nearest ancestor’s calendar. It shows in the nearest data-date-format, its own included. See Markup Dates
data-date-format a <time> with datetime Written on the <time> itself: the script writes the date from datetime in this format, in the nearest data-calendar. A <time> with neither attribute is left as written
data-date-today a <time> Written bare. The script writes the <time>’s datetime and text, and writes them again at the site’s midnight. Write no datetime. See today
data-date-era a <time> shown in Hijri Written bare. Adds the era word after the Hijri date: «هـ» on an Arabic page, AH on an English one, from the date section of the string pack

Format Tokens

Token Effect
YYYY The year, 4 digits
YY The year, 2 digits. parse reads it as 2000 to 2099
MMMM The month’s name, in locale or the page language. Only for format: parse returns null for a format with it. Keep it out of a data-date-format on <html> or a section, which Sort and Date Picker read back. On a <time>, the script only writes it
MMM The month’s short name, such as Oct, as MMMM with the same limits. A Hijri month, and a language with no short names such as Arabic, shows the full name
MM The month, 2 digits
M The month, 1 or 2 digits
DD The day, 2 digits
D The day, 1 or 2 digits
dddd The weekday’s name, such as Tuesday, in locale or the page language. Only for format, as MMMM

long in place of the tokens is the month’s name in the word order of the page language: MMMM D, YYYY in English and D MMMM YYYY in Arabic, from long_format in the date section of the string pack. data-date-format="long" and format: 'long' both take it.

Any other character is literal. A format with no day, such as MM/YYYY, parses as the first day of the month. A format with no month parses as the first month. parse returns null for a format with no year.

Options

Option Default Effect
format data-date-format, or DD/MM/YYYY The tokens to read or write. format writes ASCII digits, unless you set numerals
calendar 'gregory' The calendar of the text: 'gregory' or 'hijri', the two calendars of Saudi Arabia. 'hijri' is Umm al-Qura ('islamic-umalqura')
locale the lang of <html> The language of the text. Only for the format method: the text the browser writes, with other options and no format, and the name MMMM writes
numerals 'latn' The digits. 'arab' writes Arabic digits. Only for the format method
era false true ends a Hijri date in the era word, such as AH in English and هـ in Arabic. A Gregorian date takes none. Only for the format method, with format
any other option — Only for the format method: passed to Intl.DateTimeFormat, such as weekday, day, month, year and timeZone

JavaScript

Method Effect
NDS.Date.parse(text, options) Returns the date in text as a Date at local midnight, or null when text does not match the format or names a day that does not exist. Reads format and calendar
NDS.Date.format(date, options) Returns the date as text. With format, it fills the tokens. With other options and no format, the browser writes the text. With neither, it uses the site’s format. An invalid date returns ''
NDS.Date.getDayLabel(date, options) Returns the day in words, as a screen reader should hear it: the weekday, the day, the month’s name, the year and, for a Hijri date, the era. "Tuesday 8 Jumada al-Ula 1448 AH". Reads calendar and locale. The Date Picker and the Calendar name their days with it
NDS.Date.convert(text, from, to) Parses text with the options in from and formats it with the options in to. Returns null when text does not parse
NDS.Date.getToday() Returns today, in the site’s timezone, as a Date at local midnight
NDS.Date.readDay(datetime) Returns the day a datetime value names, as a Date at local midnight, or null. The value is YYYY-MM-DD, with or without a time. A time with a zone counts in the site’s timezone. The Calendar reads each event’s day with it
NDS.Date.toParts(date, calendar) Returns the day as numbers, { year, month, day }, in calendar (default 'gregory'). The month counts from 1 in both calendars
NDS.Date.fromParts(parts, calendar) Returns the day that { year, month, day } names in calendar, as a Date at local midnight, or null when that day does not exist
NDS.Date.getMonthLength(parts, calendar) Returns the number of days in the month that { year, month } names in calendar. A Hijri month has 29 or 30
NDS.Date.getFormat(element) Returns the nearest data-date-format around element, the element’s own included, or DD/MM/YYYY when there is none. Pass it as format for content in that part of the page
NDS.Date.getCalendar(element, fallback) Returns the nearest data-calendar around element, the element’s own included, or fallback (default 'gregory') when there is none. Pass it as calendar
NDS.Date.site Returns { timeZone, format } from <html>. timeZone is undefined when the attribute is missing or invalid
NDS.Date.getMonthNames(calendar, lang) Returns the 12 month names of 'gregory' or 'hijri', in lang or the page’s language. The Hijri names come from the date section of the string pack
NDS.Date.getWeekdayNames(lang, weekStart) Returns the 7 short weekday names in lang or the page’s language, from weekStart: 0 is Sunday (the default) and 6 is Saturday
NDS.Date.getWeekStart(element) Returns the nearest data-week-start around element, the element’s own included, as 0 (Sunday) to 6 (Saturday). Returns 0 when there is none. Pass it as weekStart
NDS.Date.readWeekdays(text) Returns the weekdays in text, words from sun to sat with spaces or commas between them, as numbers from 0 (Sunday) to 6 (Saturday): 'fri sat' gives [5, 6]. A word that is not a weekday is skipped, with a warning in the console. The Date Picker reads data-disabled-weekdays with it
NDS.Date.getStartOfWeek(date, weekStart) Returns the first day of the week that holds date, as a Date at local midnight. weekStart is 0 (Sunday) by default
NDS.Date.fill(root) Writes the text of every <time> in root, and of root itself, that has its own data-calendar, data-date-format or data-date-today, and of every empty Date Tile. The loader runs it at load, and NDS.init.mount() on markup added later
NDS.Date.fillTile(tile, options) Writes a Date Tile’s parts from its datetime and data-date-end, and adds them to an empty tile. calendar and locale default to the nearest data-calendar and lang. A Calendar calls it for each event
NDS.Date.init() Writes every markup date on the page, and follows a change of lang on <html>. The loader runs it
// Today's Hijri date as numbers, in the site's timezone. const { year, month, day } = NDS.Date.toParts(NDS.Date.getToday(), 'hijri'); // The days in that Hijri month: 29 or 30. const days = NDS.Date.getMonthLength({ year, month }, 'hijri');

The full API is in the banner of _js/nds-date.js.

Last Modified Date: 01/11/2026 - 10:48 PM
Was this page useful?
60% of users said Yes from 2843 Feedbacks