Macro to convert Gregorian calendar to other calendar (example: hijri)

For example Gregorian to Hijri : (see answer below)

This is example of the function that returns any date from Gregorian date:

Function ConvertDate( sDate As String, sCalendar As String, sLanguage As String ) As Date
	Dim UNIXEPOCH As Date
	Dim oCalendar As Object
	Dim sDay, sMonth, sYear As String
	Dim oLocale As New com.sun.star.lang.Locale
	
	UNIXEPOCH = CDate("1970-01-01")
	oLocale.Language = sLanguage
	oCalendar = CreateUnoService( "com.sun.star.i18n.LocaleCalendar" )
	oCalendar.LoadCalendar( sCalendar, oLocale )	
	oCalendar.DateTime = CDate( sDate ) - UNIXEPOCH	
	
	sDay = oCalendar.GetDisplayString( com.sun.star.i18n.CalendarDisplayCode.LONG_DAY, _
								 com.sun.star.i18n.NativeNumberMode.NATNUM0 )
	sMonth = oCalendar.GetDisplayString( com.sun.star.i18n.CalendarDisplayCode.LONG_MONTH, _
								 com.sun.star.i18n.NativeNumberMode.NATNUM0 ) 
	sYear = oCalendar.GetDisplayString( com.sun.star.i18n.CalendarDisplayCode.LONG_YEAR, _
								 com.sun.star.i18n.NativeNumberMode.NATNUM0 )	
	
	ConvertDate = CDate( sYear  & "-" & sMonth & "-" & sDay )
End Function

If you want to convert a gregorian date to Islamic date (هجري):

Sub Main
	Dim GregorianDate, ConvertedDate As Date
	Dim sCalendar, sLanguage As String
	
	GregorianDate = DateValue("2020-06-07")
	sCalendar = "hijri"
	sLanguage = "ar"
	
	ConvertedDate = ConvertDate( GregorianDate, sCalendar, sLanguage )
	
	Print ConvertedDate ' displays: 16.10.1441 which is: ‫16 شوال 1441‬
End Sub

All you need to do is provide:
Gregorian date
the type of the calendar(*) like "hijri", "jewish", etc.
and the language code which consists of two letters according to ISO 639

To see what are all the available calendar to use for a language, call getAllCalendars with a locale of your choice.
For example, if I want to see the available calendars for Japanese:

Dim oCalendar As Object
Dim availableCal as Variant
Dim oLocale As New com.sun.star.lang.Locale
oLocale.Language = "ja"

oCalendar = CreateUnoService( "com.sun.star.i18n.LocaleCalendar" )
availableCal  = oCalendar.getAllCalendars( oLocale )

Print availableCal(0) & " , " & availableCal(1)  ' This displays: gregorian , gengou

Please correct any mistakes :slight_smile:

More informations here:
https://api.libreoffice.org/docs/idl/ref/interfacecom_1_1sun_1_1star_1_1i18n_1_1XExtendedCalendar.html#aba2a0159d792b61b4690efef1fcc9152

and here:
https://docs.libreoffice.org/i18npool/html/namespacei18npool.html

1 Like

Hi, What about converting Hijri to Gregorian date? Thanks