2

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)
Justin Grant
  • 44,807
  • 15
  • 124
  • 208
  • Careful with your spelling of "JavaScript" to avoid collisions with "Java". – Basil Bourque Sep 29 '22 at 20:28
  • "fa-IR" is a BCP47 language code, not a locale, which might be Tehran or other place. – RobG Sep 29 '22 at 20:38
  • The [ECMAScript *Date* object](https://tc39.es/ecma262/multipage/numbers-and-dates.html#sec-date-objects) only supports the proleptic Gregorian calendar. If you want to parse or produce dates for any other calendar, you will have to either write your own parse and format functions, or use a library, though the *Intl.DateTimeFormat* constructor can help with formatting. – RobG Sep 29 '22 at 20:47

2 Answers2

1

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]
Justin Grant
  • 44,807
  • 15
  • 124
  • 208
0

Update

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)

Original Answer (invalid)

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"));
EssXTee
  • 1,783
  • 1
  • 13
  • 18
  • 1
    as you see "1401/01/01" is in another locale and putting it inside Date create a wrong equvalent date in gregorian(1401 is in hijri shamsi) – shahrooz bazrafshan Sep 29 '22 at 19:38
  • @kingofday What date is it supposed to be and what is it showing up as for you? I am not able to replicate what you claim. – EssXTee Sep 29 '22 at 19:40
  • @kingofday I have updated my answer. Unfortunately I don't know much about the Hijri date system. I did link to another Stack Overflow topic that seems to be able to convert between Hijri and Gregorian and this may be able to help you. By default it seems the JavaScript `Date` object doesn't work with Hijri Calendar dates though. – EssXTee Sep 29 '22 at 19:58
  • "*MDN also notes on the Date.parse() method only supports the ISO 8601 format*". MDN is wrong about that. [*Date.parse*](https://tc39.es/ecma262/multipage/numbers-and-dates.html#sec-date.parse) supports 3 formats, i.e. those produced by *toISOString*, *toString* and *toUTCString*. – RobG Sep 29 '22 at 20:42
  • from locale i mean what been used in "Intl" of javascript that convert a date object with format to another calendar, but not vise versa, thank you for your answer – shahrooz bazrafshan Sep 30 '22 at 07:44