We will be migrating from Ask to Discourse on the first week of August, read the details here

Ask Your Question

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

asked 2020-06-07 21:31:25 +0200

libre officer gravatar image

updated 2020-08-07 13:21:41 +0200

Alex Kemp gravatar image

For example Gregorian to Hijri : (see answer below)

edit retag flag offensive close merge delete

1 Answer

Sort by » oldest newest most voted

answered 2020-06-13 21:27:15 +0200

libre officer gravatar image

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 :)

More informations here: https://api.libreoffice.org/docs/idl/...

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

edit flag offensive delete link more


Hi, What about converting Hijri to Gregorian date? Thanks

vzool gravatar imagevzool ( 2020-12-09 07:22:41 +0200 )edit
Login/Signup to Answer

Question Tools

1 follower


Asked: 2020-06-07 21:31:25 +0200

Seen: 208 times

Last updated: Aug 07 '20