RingDotNetfor Ring

Other calendars

A date is one moment, but people write it in different calendars. .NET knows the Hijri (Umm al-Qura), Persian, Hebrew, Japanese and Thai Buddhist calendars. This lesson reads one day in each, converts a Hijri date back, and prints dates in Arabic.

What you will learn#

One moment, many calendars#

A .NET DateTime always stores a Gregorian date. A calendar object is a translator: you give it the DateTime and ask for the year, month and day as that calendar counts them. The classes you need are UmAlQuraCalendar (the Hijri calendar used in Saudi Arabia), HijriCalendar, PersianCalendar, HebrewCalendar, JapaneseCalendar, ThaiBuddhistCalendar and GregorianCalendar. All are in the namespace System.Globalization, so the program starts with using { "System", "System.Globalization" }.

The program#

samples/UsingDotNet/tutorials/ch03_calendars.ring
# other calendars - Hijri (Umm al-Qura), Persian, Hebrew, Japanese and Thai Buddhist dates with System.Globalization
load "dotnet.ring"

using {
	"System"
	"System.Globalization"
}

func main

	oDate = new DateTime(2024,3,11)                   # a DateTime always holds a Gregorian date; a Calendar object reads it in its own way

	# 1. The same day in several calendars. Each calendar class has GetYear, GetMonth and GetDayOfMonth.
	? "1. the day 2024-03-11 in:"
	? "   Gregorian         : " + dateText(new GregorianCalendar(),oDate)
	? "   Hijri (Umm al-Qura): " + dateText(new UmAlQuraCalendar(),oDate)
	? "   Persian           : " + dateText(new PersianCalendar(),oDate)
	? "   Hebrew            : " + dateText(new HebrewCalendar(),oDate)
	? "   Thai Buddhist     : " + dateText(new ThaiBuddhistCalendar(),oDate)
	? "   Japanese (era year): " + new JapaneseCalendar().GetYear(oDate)

	# 2. From a Hijri date to a Gregorian date: ToDateTime. The first day of Ramadan 1445.
	oHijri = new UmAlQuraCalendar()
	oRamadan = oHijri.ToDateTime([ :year = 1445, :month = 9, :day = 1, :hour = 0, :minute = 0, :second = 0, :millisecond = 0 ])
	? nl + "2. 1 Ramadan 1445 is " + oRamadan.Format("yyyy-MM-dd") + ", a " + oRamadan.DayOfWeek

	# 3. Month lengths: a Hijri month has 29 or 30 days (Umm al-Qura follows tables of the moon)
	? nl + "3. the months of 1445:"
	for nMonth = 1 to 12
		? "   month " + padLeft("" + nMonth,2) + ": " + oHijri.GetDaysInMonth([ :year = 1445, :month = nMonth ]) + " days, starts " + oHijri.ToDateTime([ :year = 1445, :month = nMonth, :day = 1, :hour = 0, :minute = 0, :second = 0, :millisecond = 0 ]).Format("yyyy-MM-dd")
	next

	# 4. Month and day names in the language and the calendar of a culture. ar-SA uses Arabic names and the Hijri calendar.
	oArabic = new CultureInfo("ar-SA")
	oEnglish = new CultureInfo("en-US")
	? nl + "4. in ar-SA the culture's own calendar is " + oArabic.Calendar.GetType().Name
	? "   " + oDate.Format([ :format = "dddd, d MMMM yyyy", :provider = oArabic ])
	? "   " + oDate.Format([ :format = "dddd, d MMMM yyyy", :provider = oEnglish ])

	# 5. How long is a year? The Hijri year is 354 or 355 days, about 11 days shorter than the Gregorian year, so Ramadan moves back
	? nl + "5. the first day of Ramadan, year after year:"
	for nYear = 1445 to 1449
		oStart = oHijri.ToDateTime([ :year = nYear, :month = 9, :day = 1, :hour = 0, :minute = 0, :second = 0, :millisecond = 0 ])
		? "   " + nYear + ": " + oStart.Format("yyyy-MM-dd")
	next

	# 6. Leap years of the Gregorian calendar
	oGregorian = new GregorianCalendar()
	? nl + "6. 2024 is a leap year: " + yesNo(oGregorian.IsLeapYear(2024)) + ", 2100: " + yesNo(oGregorian.IsLeapYear(2100)) + ", 2000: " + yesNo(oGregorian.IsLeapYear(2000))

# "1445-9-1" for a date, read in the calendar oCalendar
func dateText oCalendar, oDate

	return string(oCalendar.GetYear(oDate)) + "-" + oCalendar.GetMonth(oDate) + "-" + oCalendar.GetDayOfMonth(oDate)

func padLeft cText, nWidth

	while len(cText) < nWidth
		cText = " " + cText
	end
	return cText

func yesNo lValue

	if lValue
		return "yes"
	ok
	return "no"
Output
1. the day 2024-03-11 in:
   Gregorian         : 2024-3-11
   Hijri (Umm al-Qura): 1445-9-1
   Persian           : 1402-12-21
   Hebrew            : 5784-7-1
   Thai Buddhist     : 2567-3-11
   Japanese (era year): 6

2. 1 Ramadan 1445 is 2024-03-11, a Monday

3. the months of 1445:
   month  1: 29 days, starts 2023-07-19
   month  2: 30 days, starts 2023-08-17
   month  3: 30 days, starts 2023-09-16
   month  4: 30 days, starts 2023-10-16
   month  5: 29 days, starts 2023-11-15
   month  6: 30 days, starts 2023-12-14
   month  7: 29 days, starts 2024-01-13
   month  8: 29 days, starts 2024-02-11
   month  9: 30 days, starts 2024-03-11
   month 10: 29 days, starts 2024-04-10
   month 11: 29 days, starts 2024-05-09
   month 12: 30 days, starts 2024-06-07

4. in ar-SA the culture's own calendar is UmAlQuraCalendar
   الاثنين, 1 رمضان 1445
   Monday, 11 March 2024

5. the first day of Ramadan, year after year:
   1445: 2024-03-11
   1446: 2025-03-01
   1447: 2026-02-18
   1448: 2027-02-08
   1449: 2028-01-28

6. 2024 is a leap year: yes, 2100: no, 2000: yes

How it works#

Reading a date#

Every calendar has the same three methods, so one helper (dateText) works for all of them. 11 March 2024 is 1 Ramadan 1445 in the Umm al-Qura calendar, 21 Esfand 1402 in the Persian calendar, and the year 2567 in the Thai Buddhist calendar. The Japanese calendar counts years within an era: 2024 is year 6 of Reiwa.

Going back#

calendar.ToDateTime(year, month, day, hour, minute, second, millisecond) builds the DateTime from a date in that calendar: oHijri.ToDateTime([ :year = 1445, :month = 9, :day = 1, :hour = 0, :minute = 0, :second = 0, :millisecond = 0 ]) is 2024-03-11. Step 3 uses it to print the start of each Hijri month: the lengths come from GetDaysInMonth([ :year = 1445, :month = nMonth ]) and are 29 or 30 days.

Note

A calendar method with several forms is one Ring method that takes one value or a list of named values: GetDaysInMonth([ :year = 1445, :month = 9 ]). ToDateTime takes seven values here.

Why Ramadan moves#

The Hijri year has 354 or 355 days, about eleven days fewer than the Gregorian year. Step 5 lists the first day of Ramadan for five years: 11 March 2024, 1 March 2025, 18 February 2026... it moves about eleven days earlier each year and goes round the whole Gregorian year in about 33 years.

Names in the language of a culture#

A CultureInfo("ar-SA") has Arabic names and uses the Umm al-Qura calendar by default. oDate.Format([ :format = "dddd, d MMMM yyyy", :provider = oArabic ]) prints the weekday, day, month and year of the culture's own calendar: «الاثنين, 1 رمضان 1445». With en-US the same date prints as «Monday, 11 March 2024». The format letters come from the tutorial Numbers, dates and times and the cultures from the tutorial Cultures.

Leap years#

GregorianCalendar.IsLeapYear follows the rule every 4 years, except every 100, except every 400: 2024 and 2000 are leap years, 2100 is not.

Warning

The Umm al-Qura calendar is only defined for a range of years (about 1318–1500 Hijri, that is 1900–2077). Outside the range a calendar method raises an error. Catch it with try / catch (the tutorial When things go wrong). Real moon sighting can also differ from the tables by a day.

What you learned#

Exercises#

  1. Print today's date in the Hijri and the Persian calendar.
  2. Find how many days are left until the next 1 Ramadan.
  3. Use PersianCalendar to list the months of the current Persian year with their lengths.
  4. Show a date with Format in fa-IR and he-IL.

Next#

Working days

RingDotNet — use .NET classes and methods from Ring programs. Screenshots and output on these pages come from the real samples.