0

I'm trying to convert current date to Hijri.

This below is the function:

var fixd;

        function isGregLeapYear(year)
        {
            return year%4 == 0 && year%100 != 0 || year%400 == 0;
        }

        function gregToFixed(year, month, day)
        {
            var a = Math.floor((year - 1) / 4);
            var b = Math.floor((year - 1) / 100);
            var c = Math.floor((year - 1) / 400);
            var d = Math.floor((367 * month - 362) / 12);

            if (month <= 2)
            e = 0;
            else if (month > 2 && isGregLeapYear(year))
            e = -1;
            else
            e = -2;

            return 1 - 1 + 365 * (year - 1) + a - b + c + d + e + day;
        }

        function Hijri(year, month, day)
        {
            this.year = year;
            this.month = month;
            this.day = day;
            this.toFixed = hijriToFixed;
            this.toString = hijriToString;
        }

        function hijriToFixed()
        {
            return this.day + Math.ceil(29.5 * (this.month - 1)) + (this.year - 1) * 354 +
            Math.floor((3 + 11 * this.year) / 30) + 227015 - 1;
        }
        
        var months = ["Muharram","Safar","Rabiul Awwal","Rabiul Tsani","Jumadil Ula","Jumadil Tsani","Rajab","Sya\'ban","Ramadhan","Syawwal","Dzul Qa\'dah","Dzul Hijjah"];

        function hijriToString()
        {
            var months = new Array("Muharram","Safar","Rabiul Awwal","Rabiul Tsani","Jumadil Ula","Jumadil Tsani","Rajab","Sya\'ban","Ramadhan","Syawwal","Dzul Qa\'dah","Dzul Hijjah");
            return this.day + " " + months[this.month -1]+ " " + this.year;
        }

        function fixedToHijri(f)
        {
            var i=new Hijri(1100, 1, 1);
            i.year = Math.floor((30 * (f - 227015) + 10646) / 10631);
            var i2=new Hijri(i.year, 1, 1);
            var m = Math.ceil((f - 29 - i2.toFixed()) / 29.5) + 1;
            i.month = Math.min(m, 12);
            i2.year = i.year;
            i2.month = i.month;
            i2.day = 1;
            i.day = f - i2.toFixed() + 1;
            return i;
        }

        var weekday=new Array("Ahad","Senin","Selasa","Rabu","Kamis","Jumat","Sabtu");
        var monthname=new Array("Januari","Februari","Maret","April","Mei"," Juni","Juli","Agustus","September","Oktober","November","Desember");

        var y = date.getFullYear();
        var m = date.getMonth();
        var d = date.getDate();
        var dow = date.getDay();
        m++;
        fixd=gregToFixed(y, m, d);
        var h=new Hijri(1421, 11, 28);
        h = fixedToHijri(fixd);

Then I put this $('.hijriyah .month').html(h.toString()); to set the value.

The value is: 7 Jumadil Ula 1442

First for date, $('.hijriyah .date').html("set to date"); will be: 7
Second for month, $('.hijriyah .month').html("set to month"); will be: Jumadil Ula
Third for year, $('.hijriyah .year').html("set to year"); will be: 1442

The function is working good, but I need to separe the value to be 3 parts.

And 1 more, I need to add the day also there,

var weekday=new Array("Ahad","Senin","Selasa","Rabu","Kamis","Jumat","Sabtu");

Fourth for day, $('.hijriyah .day').html("set to day"); will be: Selasa (means Tuesday)

How to do that?

HiDayurie Dave
  • 1,791
  • 2
  • 17
  • 45

1 Answers1

1

The best and available built-in method in Javascript is to use the Intl.DateTimeFormat() constructor method.

Not only will it convert the current 'Gregorian' date to Hijri date but it will convert any Gregorian Date to any of the 18 available calendar formats.

console.log(new Intl.DateTimeFormat('en-u-ca-islamic').format(new Date("2022-02-20")));
console.log(new Intl.DateTimeFormat('en-u-ca-islamic').format(new Date(2030,7,15)));
console.log(new Intl.DateTimeFormat('en-u-ca-islamic',{dateStyle: 'full'}).format(new Date())); // current date
console.log(new Intl.DateTimeFormat('en-u-ca-islamic',{dateStyle: 'medium'}).format(new Date())); // current date
Mohsen Alyafei
  • 4,765
  • 3
  • 30
  • 42