-1

In our logs we are using Date.toString() to store events timestamps. Example:

Wed Nov 21 2018 02:04:38 GMT-0800 (Pacific Standard Time)

It is working well for us except when user's machine is in a locale other than English, then it would output timezone name in a local language (for example, Russian, Hebrew or Portugese)

Thu Nov 29 2018 16:21:07 GMT-0200 (Horário brasileiro de verão)

Is there a way to force toString() output to "EN-US" locale?

Evgeny Vinnik
  • 1,141
  • 2
  • 15
  • 26
  • [Yes, there is.](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/toLocaleString) – Herohtar Dec 14 '18 at 19:59
  • Does it always need to be "Pacific Standard Time"? – Salman A Dec 14 '18 at 20:00
  • @SalmanA No, no need to be "Pacific Standard Time", just needs to be in English. – Evgeny Vinnik Dec 14 '18 at 20:05
  • 1
    https://xkcd.com/1179/ Please make your life easier by using standard non-locale-specific format (iso 8601) – Alexei Levenkov Dec 14 '18 at 20:22
  • Possible duplicate of [How to format a JavaScript date](https://stackoverflow.com/questions/3552461/how-to-format-a-javascript-date) – Amaury Dec 14 '18 at 20:59
  • English is a language, not a locale. The format of the string generated by *Date.prototype.toString* has been standardised since [*ECMAScript 2018*](http://ecma-international.org/ecma-262/9.0/#sec-date.prototype.tostring) (the current version). The timezone name is optional and implementation dependent, it varies greatly across implementations. – RobG Dec 17 '18 at 04:39

1 Answers1

0

You can do it that way:

var options = { weekday: 'short', year: 'numeric', month: 'short', day: 'numeric', hour: '2-digit', minute: 'numeric', second: 'numeric', timeZoneName: 'short' };
new Date().toLocaleDateString("en-US", options)

Output:

"Fri, Dec 14, 2018, 3:17:54 PM EST"
Amaury
  • 106
  • 10
  • The output *toLocaleString* is not standardised, particularly the timezone name, which is the OP's issue. – RobG Dec 17 '18 at 04:48