Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
393 views
in Technique[技术] by (71.8m points)

javascript - date.toLocaleDateString conversion with locale Arabic yields different results in Chrome and firefox

I have a function that formats date using date.toLocaleDateString I see that when I have locale set to 'Arabic' the results in Chrome are different from what is on FF and Safari.

let date = new Date();
const dateOptions = {
  day: "2-digit",
  month: "2-digit",
  year: "numeric"
};

console.log(date.toLocaleDateString("ar", dateOptions));
question from:https://stackoverflow.com/questions/65924543/date-tolocaledatestring-conversion-with-locale-arabic-yields-different-results-i

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Try to use ar-eg instead of just ar:

console.log(date.toLocaleDateString("ar-eg", dateOptions));

But remember that the toLocaleDateString implentation is always application dependent, furthermore there are browsers that do not implement the locale/option parameters.

You can check the language and options support by using the following functions:

function dateSupportsLocales(language) {
  try {
    new Date().toLocaleDateString(language);
  } catch (e) {
    return e.name !== 'RangeError';
  }
  return true;
}

function dateSupportsOptions() {
  try {
    new Date().toLocaleDateString('en', {});
    return true;
  }
  catch (e) { }
  return false;
}

Check also the Date.prototype.toLocaleDateString MDN Reference for more informations.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...