Different output for Hijri dates

YasserKhalil
PlatinumLounger
Posts: 4911
Joined: 31 Aug 2016, 09:02

Different output for Hijri dates

Post by YasserKhalil »

Hello everyone

I am trying the following UDF

Code: Select all

Sub Test()
    Debug.Print Date
    Debug.Print ToHijri(Date)
End Sub

Function ToHijri(DateString As String) As String
    Dim c As VbCalendar, d As Date
    c = Calendar
    Calendar = vbCalGreg
    d = DateString
    Calendar = vbCalHijri
    ToHijri = d - 1
    Calendar = c
End Function
The output for the UDF for 20/9/2021 is 12/02/1443

If I typed the date 20/9/2021 in a cell and format cells > Date > and from Calendar type selected "Hijri", I got the correct date which is 13/02/1443

While this UDF returns the correct Hijri date

Code: Select all

Function DHijri(dtGegDate As Date) As String
    VBA.Calendar = vbCalHijri
    DHijri = dtGegDate
    VBA.Calendar = vbCalGreg
End Function

Any ideas?

User avatar
HansV
Administrator
Posts: 78393
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Different output for Hijri dates

Post by HansV »

It's probably because you use

Code: Select all

    ToHijri = d - 1
This subtracts 1 from the date...
Best wishes,
Hans

YasserKhalil
PlatinumLounger
Posts: 4911
Joined: 31 Aug 2016, 09:02

Re: Different output for Hijri dates

Post by YasserKhalil »

Thanks a lot for the clarification. I thought that too but doubted that minus one is to suit the returned result
Thank you very much.