flatpickr / flatpickr

lightweight, powerful javascript datetimepicker with no dependencies
https://flatpickr.js.org
MIT License
16.13k stars 1.45k forks source link

support for different calendars #478

Closed aboganas closed 7 years ago

aboganas commented 7 years ago

First thanks for this beautiful plugin. Ability to support multiple calendars will make this plugin even greater. If it adapt Julian Date as a common reference point, then it is easy to convert dates between the different calendars and make the plugin works for different calendars.

for example, to convert from gregorian to julian and vice versa

// Retrieve the Julian date equivalent for the Gregorian date
function GregorianToJulianDay (year, month, day) {
    // Jean Meeus algorithm, "Astronomical Algorithms", 1991
    if (month < 3) {
      month += 12;
      year--;
    }
    var a = Math.floor(year / 100);
    var b = 2 - a + Math.floor(a / 4);
    return Math.floor(365.25 * (year + 4716)) +
      Math.floor(30.6001 * (month + 1)) + day + b - 1524.5;
  };

  function JulianDayToGregorian (JulianDay) {
    // Jean Meeus algorithm, "Astronomical Algorithms", 1991
    var z = Math.floor(JulianDay + 0.5);
    var a = Math.floor((z - 1867216.25) / 36524.25);
    a = z + 1 + a - Math.floor(a / 4);
    var b = a + 1524;
    var c = Math.floor((b - 122.1) / 365.25);
    var d = Math.floor(365.25 * c);
    var e = Math.floor((b - d) / 30.6001);
    var day = b - d - Math.floor(e * 30.6001);
    var month = e - (e > 13.5 ? 13 : 1);
    var year = c - (month > 2.5 ? 4716 : 4715);
    d = year+"/"+month+"/"+day;
    return d;
  },

and here to convert from Islamic-Hijri to julian and vice versa

// Julian date of start of Islamic epoch: 16 July 622 CE.
jdEpoch: 1948439.5,
// days in months for common hijri year
daysInMonth: [30, 29, 30, 29, 30, 29, 30, 29, 30, 29, 30, 29],

// Retrieve the Julian date equivalent for the Hijri date
function HijriToJulianDay (year, month, day) {
    return day + Math.ceil(29.5 * (month - 1)) + (year - 1) * 354 +
      Math.floor((3 + (11 * year)) / 30) + this.jdEpoch - 1;
  };

// Retrieve the Hijri date equivalent for the Julian date
function JulianDayToHijri (JulianDay) {
    JulianDay = Math.floor(JulianDay) + 0.5;
    var year = Math.floor((30 * (JulianDay - this.jdEpoch) + 10646) / 10631);
    year = (year <= 0 ? year - 1 : year);
    var month = Math.min(12, Math.ceil((JulianDay - 29 - this.HijriToJulianDay(year, 1, 1)) / 29.5) + 1);
    var day = JulianDay - this.HijriToJulianDay(year, month, 1) + 1;
    var d = year+"/"+month+"/"+day;
    return d;
  };

//days in hijry month
function getDaysinHijriMonth (month, year) {
  return this.daysInMonth[month-1] +
    (month === 12 && this.leapYear(year) ? 1 : 0);
};

function leapYear (year) {
  return (year * 11 + 14) % 30 < 11;
};

**My question what functions that need to be defined for a calendar for the plugin to work correctly? for example:

How hard to achieve such thing?

chmln commented 7 years ago

While it'd be nice to have, this feature will go unused for ~99% of users. You are free to fork the calendar and make any changes necessary :+1:

dalwadani commented 7 years ago

Sorry to reopen this issue but can we have something like this as a compromise: jan-2016

This can be added as a function to allow adding any arbitrary text under each date just like:

document.getElementById("#subtextCustom").flatpickr({
    subtext: [
        function(dateObj){
            // Code to add alternative date under
            return getHijri(dateObj);
        }
    ]
});