2

I am using Intl package and try to format dates per locales. For ar-AE example, it formats date using arabic characters but I want to print it using numeric value.

ops = {
    month: 'long',
    day  : 'numeric',
    year : 'numeric'
  };
console.log(new Intl.DateTimeFormat('ar-AE', ops).format(date));

It prints ٢ يناير، ٢٠١٧ but I want something like 2017/01/02 or "الاثنين، 2 يناير، 2017", Is this possible?

One more question - Is that possible to format using custom pattern? If I have a localized pattern, can I use the pattern and format the date?

codereviewanskquestions
  • 13,460
  • 29
  • 98
  • 167

2 Answers2

3

This is normal, because you are asking for ar-AE standard. You want the moroccan standard: ar-MA:

var date = new Date();
ops = {month: "long", day: "numeric", year: "numeric"};

console.log(new Intl.DateTimeFormat('ar-AE', ops).format(new Date()));
// ١٤ يناير، ٢٠١٨
console.log(new Intl.DateTimeFormat('ar-MA', ops).format(new Date()));
// 14 يناير، 2018

Check at examples from moment.js . They give a nice overview of date standards.

forzagreen
  • 2,509
  • 30
  • 38
  • Why `console.log(new Intl.DateTimeFormat('ar').format(new Date()));` outputs: `20‏/12‏/2020` It looks like it's messed up, or is it the correct format for dates in the Arabic language? – tonix Dec 20 '20 at 13:02
1

To get numbers to be printed in 'latn' irrespective of the locale used, then add the numbering system using u-nu-latn after the locale.

It will change the digits to Latin numbers but will keep the remaining output as per the locale. Please note the RTL direction of the output string.

let ops = {
    month: 'long',
    day  : 'numeric',
    year : 'numeric'
  };
console.log(new Intl.DateTimeFormat('ar-AE-u-nu-latn',ops).format(new Date()));
Mohsen Alyafei
  • 4,765
  • 3
  • 30
  • 42