3

I am located in the PDT timezone. When I type in new Date() into the browser's console, I get Tue Aug 31 2021 09:43:15 GMT-0700 (Pacific Daylight Time).

Now, I want the date in the same format from the central time region.

So I want the time object to look like this: Aug 31 2021 11:43:15 GMT-0500 (Central Daylight Time)

I've been reading the docs about Intl, Intl.DateTimeFormat() and toLocalTimeString() but can't seem to find it.

I tried new Date().toLocaleString(undefined, { timeZone: "US/Central" }) which gives me "8/31/2021, 11:46:30 AM", which is correct but not the right format.

I tried new Intl.DateTimeFormat('en-US', { dateStyle: 'full', timeStyle: 'long', timeZone:"US/Central" }).format(new Date()) which also gives me a string and not a date object in UTC format.

What am I doing wrong?

Tom
  • 2,545
  • 5
  • 31
  • 71
  • 2
    There is no built–in method to do that in a simple way. But you can use [*formatToParts*](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/DateTimeFormat/formatToParts) then arrange them yourself (e.g. [this answer](https://stackoverflow.com/a/61721327/257182)). Also see [*How to format a JavaScript date?*](https://stackoverflow.com/questions/3552461/how-to-format-a-javascript-date?r=SearchResults&s=1|1919.3661) – RobG Aug 31 '21 at 20:21
  • 1
    BTW, [UTC is a time standard](https://en.wikipedia.org/wiki/Coordinated_Universal_Time), not a format. The format you want is defined by ECMA-262 as that produced by [*Date.prototype.toString*](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/toString). – RobG Aug 31 '21 at 20:41

1 Answers1

3

Date in JS is a tricky issue - I agree 100% with you:

I did it once the hard way:

  1. splitting the resulting date object into parts
  2. concat all the parts according to my desired custom format

Maybe not the most elegant way, but it worked. Any suggestions from other contributors are very welcome.

Edit:
Added let oTZPartsReduced = ... in reply to valuable comment from @RobG

let myTimeZone = new Date();
console.log(myTimeZone);
//console.log((myTimeZone.getTimezoneOffset()/60).toFixed(2));

// oTZ = otherTimeZone  (to keep it short)
let oTZ = new Date(myTimeZone).toLocaleString("en-US", {timeZone: "US/Central"});

let oTZFormatted;

let oTZParts = new Intl.DateTimeFormat('en', {
  weekday: 'short',
  day:'2-digit',
  month: 'short',
  year: 'numeric',
  hour: '2-digit',
  minute: '2-digit',
  second: '2-digit',
  timeZone: 'US/Central' // <-- here it goes, your wished timeZone
}).formatToParts(new Date(myTimeZone));

oTZFormatted = oTZParts[2].value + ' ' + oTZParts[4].value + ' '+ oTZParts[6].value + ' '+ oTZParts[8].value + ':'+ oTZParts[10].value + ':'+ oTZParts[12].value + ' GMT-0500 (Central Daylight Time)';

// order within above array could vary due to language as @RobG stated - thanks
// hence following reduced version of above array:
let oTZPartsReduced = oTZParts.reduce((acc, part) => {
  if (part.type != 'literal') {
    acc[part.type] = part.value;
  }
  return acc;
}, Object.create(null));

oTZFormatted = oTZPartsReduced.month + ' ' + oTZPartsReduced.day + ' '+ oTZPartsReduced.year + ' '+ oTZPartsReduced.hour + ':'+ oTZPartsReduced.minute + ':'+ oTZPartsReduced.second + ' GMT-0500 (Central Daylight Time)';

console.log(oTZFormatted);
console.log('Array of Parts for reference:');
console.log(oTZParts);
console.log('Object (reduced) of Parts for reference:');
console.log(oTZPartsReduced);
Jürgen Fink
  • 3,162
  • 2
  • 23
  • 25
  • 2
    The order of the parts is determined by the language so may change (e.g. en-US, en-GB, en-CA are all different). Consider using *reduce* to convert the array to an object with `{part.type: part.value}`, e.g. [here](https://stackoverflow.com/a/61721327/257182). The only duplicate types/names are the literals, which most don't care about. – RobG Sep 01 '21 at 00:03
  • @RobG Thank you for your valuable comment - excelent. I edited my code accordingly, so now we have both versions: _array_ and _reduced to object_. Again thanks and a big upvote for that – Jürgen Fink Sep 01 '21 at 16:38