I have year, month and day like:
let dt = '1401/01/01'
in locale like:
let locale = 'fa-IR'
how can create a date object from it without using some libraries?
something like:
new Date(dt,locale)
I have year, month and day like:
let dt = '1401/01/01'
in locale like:
let locale = 'fa-IR'
how can create a date object from it without using some libraries?
something like:
new Date(dt,locale)
The Temporal API, which is soon to be part of JavaScript, can help with this use case.
While Temporal currently requires a polyfill (see list of Temporal polyfills here), Temporal is currently being implemented in all major browsers (Chrome, Firefox, Safari, etc.) so in the not-too-distant future there will be no polyfill needed.
Assuming that you're using the persian
calendar (see list of supported calendars here) you can do something like this:
// Temporal polyfill. See https://github.com/tc39/proposal-temporal#polyfills
// has a list of actively-developed polyfills.
// Replace this import with whatever is required by your favorite polyfill.
import { Temporal } from '@js-temporal/polyfill';
let dt = '1401/01/01';
const [year, month, day] = dt.split('/');
// Create a Temporal.PlainDate object, interpreting year, month, and day
// according to the Persian calendar.
const date = Temporal.PlainDate.from({year, month, day, calendar: 'persian'});
// Format this date in Farsi, using the Persian calendar
console.log (date.toLocaleString('fa-IR', { calendar:'persian' }));
// => ۱۴۰۱/۱/۱
// Format this date in English, using the Persian calendar
console.log (date.toLocaleString('en-IR', { calendar:'persian' }));
// => 1/1/1401 AP
// Format this date in English, using the Gregorian calendar
console.log (date.withCalendar('gregory').toLocaleString('en-US', { calendar:'gregory' }));
// => 3/21/2022
// Format this date into a cross-platform, computer-readable format
console.log (date.toString());
// => 2022-03-21[u-ca=persian]
It seems the question is about Islamic (Hijri) Calendar Dates, which unfortunately I do not know a whole lot about.
However it seems the JavaScript Date
object will present difficulties as it specifies an object measured by the number of milliseconds that have elapsed since January 1, 1970. And so its basis is in the Gregorian calendar system.
MDN also notes on the Date.parse()
method only supports the ISO 8601 format, which also seems like it presents a problem for the different calendars.
There is also this post on Stack Overflow that seems to work on a very similar problem using the Intl.DateTimeFormat()
to work between the two calendars, and may provide the answer needed by OP here.
Lastly, a new method for dates in JavaScript Temporal is (as of September 29, 2022) still in Stage 3 proposal, but likely will become part of the spec in the near future. It seems Temporal
fixes a lot of the short-comings of the Date
object and should allow for what OP is asking here (as the calendar can be specified)
The JavaScript Date
object does not take a locale as a parameter. However you can use methods like toLocaleString
and toLocaleDateString
to output a date in a certain locale.
So if you just want to create a Date
object, you only need to pass in the date/time:
let dt = new Date("1401/01/01");
From there, you can preform any date related calculations or methods on the newly created Date
object. If you need to output the date in a particular locale you can use toLocaleString
:
let dt = new Date("1401/01/01");
console.log(dt.toLocaleString("fa-IR"));