Niranjan Godbole
Niranjan Godbole

Reputation: 2175

how to convert javascript date to hijri format?

Hi I am developing web application in Angularjs/JavaScript. I have one calender picker and I am capturing dates from the front end. When saving selected date in database I want to save in Hijri format. I followed Converting Gregorian date to Hijri date, it works fine when I pass 1 in the writeIslamicDate. For example:

var today=new Date();
writeIslamicDate(today);

If I pass today date then it gives me undefined, NaN undefined NaN AH so may I know do I need to convert to any format before passing input to writeIslamicDate? Can someone help me in this? Any help would be appreciated. Thank you.

Upvotes: 6

Views: 15489

Answers (3)

kerbrose
kerbrose

Reputation: 1185

The easiest way to get this conversion is to use the browser Intl object (read more). an example would be something like the following:

a = new Date();
localeFormat= 'ar-SA-islamic-umalqura';
Intl.DateTimeFormat(localeFormat).format(a)

Upvotes: 3

RobG
RobG

Reputation: 147363

All you need is a slight adjustment to the function you linked. To allow input of a date an optional adjustment, you need to allow passing in a date so make the following changes:

function writeIslamicDate(adjustment) {
  ...
  var iDate = kuwaiticalendar(adjustment);

to

function writeIslamicDate(date, adjustment) {
  ...
  var iDate = kuwaiticalendar(date, adjustment);

and

function kuwaiticalendar(adjust) {
  var today = new Date();

to:

function kuwaiticalendar(date, adjust) {
  var today = date || new Date();

And call it with either a date or not. Below is the full modified code (cleaned up a little). You can also try toLocaleString with options, which can not only convert to Islamic calendar, but also use the default language of the host.

// Returns mathematic mod, not javascript mod
// e.g. gmod(-3, 2) returns 1, whereas -3%2 returns -1
function gmod(n, m){
  return ((n % m) + m) % m;
}

/* @param {Date}   date   - optional, default is today
** @param {number} adjust - optiona, days to adjust date by
*/
function kuwaiticalendar(date, adjust) {
  var today = date? new Date(+date) : new Date();
  if (adjust) {
    today.setDate(today.getDate() + +adjust);
  }

  var day = today.getDate();
  var month = today.getMonth();
  var year = today.getFullYear();
  var m = month + 1;
  var y = year;

  if (m < 3) {
      y -= 1;
      m += 12;
  }

  var a = Math.floor(y / 100);
  var b = 2 - a + Math.floor(a / 4);

  if (y < 1583) b = 0;
  if (y == 1582) {
    if (m > 10)  b = -10;
    if (m == 10) {
      b = 0;
      if (day > 4) b = -10;
    }
  }

  var jd = Math.floor(365.25*(y+4716))+Math.floor(30.6001*(m+1))+day+b-1524;

  b = 0;
  if(jd > 2299160){
    a = Math.floor((jd - 1867216.25) / 36524.25);
    b = 1+a-Math.floor(a / 4);
  }

  var bb = jd+b+1524;
  var cc = Math.floor((bb - 122.1) / 365.25);
  var dd = Math.floor(365.25 * cc);
  var ee = Math.floor((bb - dd) / 30.6001);
  day = (bb - dd) - Math.floor(30.6001 * ee);
  month = ee - 1;

  if (ee > 13) {
    cc += 1;
    month = ee - 13;
  }
  year = cc - 4716;
  var wd = gmod(jd + 1, 7) + 1;

  var iyear = 10631./30.;
  var epochastro = 1948084;
  var epochcivil = 1948085;

  var shift1 = 8.01 / 60.;

  var z = jd-epochastro;
  var cyc = Math.floor(z/10631.);
  z = z - 10631 * cyc;
  var j = Math.floor((z - shift1) / iyear);
  var iy = 30 * cyc + j;
  z = z - Math.floor(j * iyear + shift1);
  var im = Math.floor((z + 28.5001) / 29.5);

  if (im == 13) im = 12;
  var id = z-Math.floor(29.5001*im-29);

  return [
    day,       //calculated day (CE)
    month - 1, //calculated month (CE)
    year,      //calculated year (CE)
    jd - 1,    //julian day number
    wd - 1,    //weekday number
    id,        //islamic date
    im - 1,    //islamic month
    iy         //islamic year
  ];
}

function writeIslamicDate(date, adjustment) {
  var wdNames = ["Ahad", "Ithnin", "Thulatha", "Arbaa", "Khams", "Jumuah", "Sabt"];
  var iMonthNames = ["Muharram", "Safar", "Rabi'ul Awwal", "Rabi'ul Akhir", "Jumadal Ula", "Jumadal Akhira",
                     "Rajab", "Sha'ban", "Ramadan", "Shawwal", "Dhul Qa'ada", "Dhul Hijja"];
  var iDate = kuwaiticalendar(date, adjustment);
  var outputIslamicDate = wdNames[iDate[4]] + ", " + iDate[5] + " " +
                          iMonthNames[iDate[6]] + " " + iDate[7] + " AH";
  return outputIslamicDate;
}

// No date or adjust
console.log(writeIslamicDate());
// No date, adjust to tomorrow
console.log(writeIslamicDate(undefined, 1));
// Date for 1 Jan 2017
console.log(writeIslamicDate(new Date(2017,0,1)));
// Date for 1 Jan 2017, adjust by -1 day
console.log(writeIslamicDate(new Date(2017,0,1), -1));

Using toLocaleString you'd do something like the following.

// date is optional, defaults to today
function writeHijri(date, lang) {
  var date = date || new Date();
  lang = lang || 'en';
  var options = {
    year: 'numeric', month: 'long', day: 'numeric'
  };
  return date.toLocaleString(lang + '-u-ca-islamic', options);
}

// Today in English
console.log(writeHijri());

// 1 Jan 2017 converted to Hijri (3 Rab. II 1438) in Arabic
console.log(writeHijri(new Date(2017,0,1), 'ar'));

Upvotes: 9

hame-dhib
hame-dhib

Reputation: 428

you can use moment-hijri.js it a robust library to convert date

Git Repo

moment('2014-11-28 16:40:00', 'YYYY-M-D HH:mm:ss').endOf('iMonth').format('iYYYY/iM/iD HH:mm:ss'); // 1436/2/30 23:59:59

JSFIDDEL Exemple

Upvotes: 6

Related Questions