Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to format date into Arabic with English numerals for date and year in JavaScript?

I want the date to be formatted in the following format:

date: 2016-12-04 into الأحد، 4 ديسمبر، 2016

But I'm getting the following. The numbers are converted to Arabic numerals. I want the date and year in English numerals:

الأحد، ٤ ديسمبر، ٢٠١٦

I have used the following code to format the date to Arabic.

var date = new Date('2016-12-04');
options = {
           weekday: 'long',
           year: 'numeric',
           month: 'short',
           day: 'numeric',
          };
var dateString = date.toLocaleDateString('ar-SA', options);
alert(dateString);
like image 754
Shebin Avatar asked Nov 29 '16 06:11

Shebin


People also ask

How do you write dates in Arabic format?

Short dates are written in "Day/month/year" from; when writing in Arabic and English: For Gregorian calendar: ١٦/١٢/٢٠١٣ميلادي or 16/12/2013.


2 Answers

Found a solution Date conversion and manipulation in javascript with Arabic months

My modified code to get my desired output is as follows.

var date = new Date('2016-12-04');
var months = ["يناير", "فبراير", "مارس", "إبريل", "مايو", "يونيو",
  "يوليو", "أغسطس", "سبتمبر", "أكتوبر", "نوفمبر", "ديسمبر"
];
var days = ["اﻷحد", "اﻷثنين", "الثلاثاء", "اﻷربعاء", "الخميس", "الجمعة", "السبت"];
var delDateString = days[date.getDay()] + ', ' + date.getDate() + ' ' + months[date.getMonth()] + ', ' + date.getFullYear();

console.log(delDateString); // Outputs اﻷحد, 4 ديسمبر, 2016
like image 72
Shebin Avatar answered Nov 06 '22 07:11

Shebin


You can use this Locales to specify numbering system.

let date = newDate.toLocaleDateString('ar-EG-u-nu-latn',{weekday: 'long', year: 'numeric', month: 'short', day: 'numeric'});

also check this MDN link to know more about it

like image 4
Omar Abdelhady Avatar answered Nov 06 '22 08:11

Omar Abdelhady