I created a date object with Keith Wood calendar library with Persian instant:
var d = $.calendars.newDate(1393, 5, 6, 'persian', 'fa');
Now I need to get Julian or Gregorian date from this date but when I use .toJD()
function it returns Julian date that does not equal to current date in
var e = d.toJD();
console.log(e)
so how do i fix this problem? I've created a jsbin for this problem .
I think toJD()
is not what you need:
var d = $.calendars.newDate(1388, 1, 1, 'persian', 'fa');
console.log("Persian date: "+d.toLocaleString()); // Persian date: 1388-01-01
var e = d.toJSDate();
console.log(e); // Sat Mar 21 2009 00:00:00 GMT+0100 (Romance Standard Time)
UPDATE: For your comment I see the issue is not solved with this because it transforms the Persian date to your locale date. As far as I can see in the library's reference, there is no way to tell to that function which locale should be used. So the correct way, as the author's provided demo shows, is creating another target calendar and pass the current one as parameter:
var d = $.calendars.newDate(1388, 1, 1, 'persian', 'fa');
var e = $.calendars.newDate(d, 'gregorian', 'fa');
An alternative to converting a Persian date to any other calendar date is to use the ECMAScript proposal "Temporal" which is currently in Stage 3 of Active Proposals.
The following function will convert a Persian date to any of the 18 available calendars in Javascript. The default is conversion to the Gregorian date.
You can specify the target Calendar to convert to and also the formatting and locale to use:
<script type='module'>
// ====== load Temp polyfill (not needed after implementation) ========
import * as TemporalModule from 'https://cdn.jsdelivr.net/npm/@js-temporal/[email protected]/dist/index.umd.js'
//=====================================================================
//---------------------------------------
function persianToCalendars(Y, M, D, options={}) {
return new Intl.DateTimeFormat(options.locale??="en",options).format(new Date(temporal.Temporal.PlainDateTime.from({year:Y,month:M,day:D,calendar:'persian'}).toString().split("[")[0]));
}
//---------------------------------------
console.log(persianToCalendars(1400,12,6)); // default
console.log(persianToCalendars(1400,12,6,{dateStyle: "full"}));
console.log(persianToCalendars(1400,12,6,{locale: "fa"}));
console.log(persianToCalendars(1400,12,6,{dateStyle:"full", locale: "fa"}));
console.log(persianToCalendars(1400,12,6,{calendar:"islamic", dateStyle:"full"})); // to islamic full 'en' locale
console.log(persianToCalendars(1400,12,6,{locale:"fa", calendar:"islamic", dateStyle:"full"})); // to islamic 'fa' locale
</script>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With