One day difference in Converting Gregorian to Hijri date using Intl.DateTimeFormat

53 views Asked by At

I am trying to convert Gregorian to Hijri date using Intl.DateTimeFormat, But every time it converts one day ahead. For example today is 11 Ramadan but every time it results in 12 Ramadan.

I have tried all calendar types "islamic, islamic-umalqura, islamic-civil, islamic-rgsa" I have also tried to change locale "PK, IN, AF, SA" but no difference. Is that a bug or I am doing something wrong.

console.log(new Intl.DateTimeFormat(
  'en-PK-u-ca-islamic', {
    day: 'numeric',
    month: 'long',
    weekday: 'long',
    year: 'numeric'
  }).format(new Date(2024, 2, 22)));  // 22 Mar 2024

2

There are 2 answers

5
Mburu Njoroge On

Results

Try this

const currentDate = new Date();
currentDate.setTime(currentDate.getTime() + (currentDate.getTimezoneOffset() * 60 * 1000) + (5.5 * 60 * 60 * 1000));
const islamicDate = new Intl.DateTimeFormat('en-PK-u-ca-islamic', {
  day: 'numeric',
  month: 'long',
  weekday: 'long',
  year: 'numeric'
}).format(currentDate);

console.log(islamicDate);

2
Emsil Neumann On
// just a simple example

const HijriDate = require('date-hijri');
const hijriDate = new HijriDate(Date.now());
console.log(`${hijriDate.getDate()} ${hijriDate.getMonthName()} ${hijriDate.getFullYear()}`);

Using a dedicated library like date-hijri will be more reliable for your conversion