Just make a slight adjustment to the function provided in the link. To allow input of a date and an optional adjustment, you need to modify the function as follows:
function writeIslamicDate(adjustment) {
...
var iDate = kuwaiticalendar(adjustment);
Change it to:
function writeIslamicDate(date, adjustment) {
...
var iDate = kuwaiticalendar(date, adjustment);
Also, update this part:
function kuwaiticalendar(adjust) {
var today = new Date();
to:
function kuwaiticalendar(date, adjust) {
var today = date || new Date();
You can now call the function with either a date or without one. Below is the improved code snippet (slightly cleaned up). You may also experiment with toLocaleString along with options for language customization.
// Ensures positive modulo result
// Example: gmod(-3, 2) returns 1 instead of -1
function gmod(n, m){
return ((n % m) + m) % m;
}
/* @param {Date} date - optional, defaults to current date
** @param {number} adjust - optional, days to adjust the date by
*/
function kuwaiticalendar(date, adjust) {
var today = date ? new Date(+date) : new Date();
if (adjust) {
today.setDate(today.getDate() + +adjust);
}
// Rest of the function remains unchanged
}
// Existing function slightly modified
function writeIslamicDate(date, adjustment) {
// Remaining code for the function writeIslamicDate not shown here
}
// Examples of using the functions...
console.log(writeIslamicDate());
console.log(writeIslamicDate(undefined, 1));
console.log(writeIslamicDate(new Date(2017,0,1)));
console.log(writeIslamicDate(new Date(2017,0,1), -1));
For utilizing toLocaleString, consider the following approach.
// date is optional, defaults to current date
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);
}
// Examples showing the usage of writeHijri function
console.log(writeHijri());
console.log(writeHijri(new Date(2017,0,1), 'ar'));