How to convert Date object to timezone specific?

Hello fellas,

I tried to use Intl.DateTimeFormat, but it isn’t available. What’s the alternative?

Thanks in advance,

Could you please explain what you are trying to achieve? It’s likely that this can be accomplished using operators

Sure. I need to:

  1. Extract some information from an email (gmail)
  2. it comes in HTML format so decoded the file using JS Node. As the base64 function encodes, but doesn’t decodes.
  3. Extracted the information using JSDOM.
  4. Convert the date (internalDate) from unix epoch (UTC) to a human readable date localized in Mexico City.

Steps 1 to 3 are already covereted. I’m working in step 4.

Regards,

Alfredo

Just figured it out how to do it. Here is the code for future references:

const internal_date = new Date(parseInt(data["{{8.`value`.`internalDate`}}"]));
const mx_date = internal_date.toLocaleString('es-MX', { timeZone: 'America/Mexico_City' });

Regards,

1 Like

I’ve been using Intl.DateTimeFormat without problem in JavaScript nodes. Here’s a snippet


  // Use the timestamp_override if it is not empty, otherwise use the current timestamp
  const currentTimestamp = timestampOverride && timestampOverride !== "null" ? new Date(parseInt(timestampOverride,10)).getTime() : Date.now();
  //console.log(currentTimestamp);

  // Subtract 24 hours in milliseconds
  const timestamp24HoursAgo = currentTimestamp - 24 * 60 * 60 * 1000;
  //console.log(timestamp24HoursAgo);

  // Format the date of the previous day as "mm/dd/YYYY"
  const previousDate = new Date(timestamp24HoursAgo);
  //console.log(previousDate);

  const formattedPreviousDate = `${String(previousDate.getMonth() + 1).padStart(2, '0')}/${String(previousDate.getDate()).padStart(2, '0')}/${previousDate.getFullYear()}`;
  //console.log(formattedPreviousDate);

  // Create a timezone-aware formatted date for central Time Zone
  const centralTimeFormatter = new Intl.DateTimeFormat('en-US', {
    timeZone: 'America/Chicago',
    year: 'numeric',
    month: '2-digit',
    day: '2-digit'
  });
  const centralTimeFormattedPreviousDate = centralTimeFormatter.format(previousDate);

1 Like

You are right Intl.DateTimeFormat is indeed available. I must have misread one of the errors shown in the console.

1 Like