Javascript Date toLocaleDateString()

Introduction

Javascript Date toLocaleDateString() displays the date's day of the week, month, day of the month, and year in an implementation- and locale-specific format.

The output of this method varies from browser to browser.

dateObj.toLocaleDateString([locales [, options]])
var date = new Date(Date.UTC(2020, 11, 12, 3, 0, 0));

console.log(date.toLocaleDateString());//from   w w w. j  a v a  2s. c  o  m
var date = new Date(Date.UTC(2020, 11, 20, 3, 0, 0));
console.log(date.toLocaleDateString('en-US'));
// British English uses day-month-year order
console.log(date.toLocaleDateString('en-GB'));
// Korean uses year-month-day order
console.log(date.toLocaleDateString('ko-KR'));
// Event for Persian, It's hard to manually convert date to Solar Hijri
console.log(date.toLocaleDateString('fa-IR'));
// Arabic in most Arabic speaking countries uses real Arabic digits
console.log(date.toLocaleDateString('ar-EG'));
// for Japanese, applications may want to use the Japanese calendar,
console.log(date.toLocaleDateString('ja-JP-u-ca-japanese'));

Using options

The results provided by toLocaleDateString() can be customized using the options argument:

var date = new Date(Date.UTC(2012, 11, 20, 3, 0, 0));

// request a weekday along with a long date
var options = { weekday: 'long', year: 'numeric', month: 'long', day: 'numeric' };
console.log(date.toLocaleDateString('de-DE', options));
// use UTC/*from w w  w . j a va2 s  .  c o  m*/
options.timeZone = 'UTC';
options.timeZoneName = 'short';
console.log(date.toLocaleDateString('en-US', options));



PreviousNext

Related