0

The official calendar of our country is jalali! Jalali is a type of calendar that has a mathematical relationship with the Gregorian calendar. I want to change Date() in JS to returns jalali values. there are many lib or func for this, but I don't want use them. Can I redefine Date()? Where can I view Date() source?

  • 2
    Date is a built-in object provided by the environment. Depending on where you run your code, it might not even be implemented in JS. So to "redefine" the code for it, you'd need to change V8 itself, for example. With that said, it is ***a very bad idea*** to change built-ins. You'd affect anything else that uses `Date` and you can end up breaking your whole environment. Instead you *should* be using a library or another custom implementation. There is no need or reason to change built-ins. – VLAZ Jul 28 '21 at 05:21
  • thank you, using from lib is standard method! but I don't want to write a project from scratch! the project is complete and working properly and is very big! can I define class Date() and include in html files? – erfan bakhtiari Jul 28 '21 at 05:57

2 Answers2

0

You can use toLocaleDateString();

let today = new Date().toLocaleDateString('fa-IR');
console.log(today);

fa-IR is for Farsi-Iran, but all the ISO country codes can be found here

also you can set options as second argument, for example:

let options = { year: 'numeric', month: 'long', day: 'numeric' };
new Date().toLocaleDateString('fa-IR', options);
Jesper
  • 1,007
  • 7
  • 24
  • I test it, but this method only returns the Date string, I want change var of Date! for example when use "getFullYear()" return 2021, but I want to return 1400 (this year in jalali date) – erfan bakhtiari Jul 28 '21 at 05:33
  • 1
    The OP wants to use a particular calendar, not language so better to use the required language code with calendar option, e.g. for English: `new Date().toLocaleString('en-GB-u-ca-persian')` which for today gives something like "06/05/1400 AP, 16:51:33". – RobG Jul 28 '21 at 06:51
  • i have to say that is pretty nifty! @RobG – Jesper Jul 28 '21 at 07:05
0

Don't mess with objects you don't own. You can create your own date object called maybe jDate (after "jalali date", which I assume is the same as the Intl object's "persian" calendar) and implement methods there.

The Intl.DateTimeFormat constructor returns an object that has a formatToParts method that you can leverage to implement the Date methods you need, then you can work on a standard Date object underneath but return Jalali values from the methods. e.g. to get all the current date parts in English:

let f = new Intl.DateTimeFormat('en-GB-u-ca-persian',{
  year: 'numeric',
  month: 'long',
  day: 'numeric',
  weekday: 'long',
  hour: 'numeric',
  minute: 'numeric',
  second: 'numeric',
  hour12: false,
});
console.log('Current Jalali date: ' + f.format(new Date()));

console.log('The parts:');  
f.formatToParts(new Date()).forEach(part => console.log(part.type + ': ' + part.value));

For some things you have to run the format method more than once with different options, e.g. to get both the month name and number as both are specified by the month option: month: 'long' for the name and month: 'numeric' for the number.

RobG
  • 142,382
  • 31
  • 172
  • 209