skip to Main Content

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

Answers


  1. 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);
    Login or Signup to reply.
  2. // 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

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search