Date.toLocaleFormat() has been removed

Published: | Categories: JavaScript

Description

The support for the Date.prototype.toLocaleFormat method, deprecated since Firefox 55, has been removed with Firefox 58, because it failed to be standardized. No other browsers than Firefox implement 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');

References