Date.toLocaleFormat() has been deprecated

Published: | Categories: JavaScript

Description

The support for the non-standard Date.prototype.toLocaleFormat method is now considered deprecated and will be removed in the near future. Firefox 55 and later shows a warning in the Console for the method.

There are JavaScript libraries like Sugar to achieve the same results. You can also use the standard toLocaleDateString method, which is a part of the ECMAScript Internationalization API, as the following examples illustrate:

// Formats like "Tuesday, August 1, 2017"
(new Date()).toLocaleFormat('%A, %B %e, %Y');
// can be replaced with
(new Date()).toLocaleString('en-US', {
  year: 'numeric',
  month: 'long',
  day: 'numeric',
  weekday: 'long',
});
// Formats like "2017-08-01 20:07:13"
(new Date()).toLocaleFormat('%Y-%m-%d %H:%M:%S');
// can be replaced with
(new Date()).toLocaleString('en-US', {
  year: 'numeric',
  month: '2-digit',
  day: '2-digit',
  hour: '2-digit',
  minute: '2-digit',
  second: '2-digit',
  hour12: false,
}).replace(/(\d+)\/(\d+)\/(\d+),\s(.*)/, '$3-$1-$2 $4');

Note that the Intl API is available on Firefox 56 and later on Android, while the desktop version of Firefox has already added the support with Firefox 29.

Update: This method has been removed with Firefox 58.

References