Home > Enterprise >  Change Date object from EST to PST (one example) without offset then alter it to UTC with offset. Wi
Change Date object from EST to PST (one example) without offset then alter it to UTC with offset. Wi

Time:05-21

Scenario:

Using Google Forms, a user enters a date and a time and a timezone in different fields. Google forms then outputs to Google Sheets.

I end up with a date and time combined in one column, and the three character representation of a timezone (EST, PST, CST). This gets passed to a trigger script to pass data to an external source.

Problem:

Google Sheets, defaults the date time combination to whatever that Column is set to in sheets. Which if unset is EST it seems. So whether the user filling out the form says for example: 05/05/2020 17:00:00 and they select PST from the other form field. The script will get a date/time object that contains the correct time, but its setup with EST.

The desired outcome

I want to take the correct date/time 05/05/2020 17:00:00 and change the timezone on it to PST in this example without applying an offset to it. So I can then ultimately take that date/time and apply an offset for UTC to it, so it represents the time the user input, while fulfilling the needs from storage of that data needs.

TLDR;

  • I need to take a Timestamp in EST 05/05/2020 17:00:00
  • Convert it to PST 05/05/2020 17:00:00 (so the date/time stays the same)
  • So I can then change it to equal UTC 05/06/2020 01:00:00

Without the use of a library like moment.js I need this to be pure javascript.

CodePudding user response:

This is a difficult thing to achieve with native JavaScript Dates... but maybe not impossible.

I'd suggest creating a formatISODateWithOffset() function that takes year, month, day etc. arguments along with a timezone to specify a given date in a certain location.

Once this timestamp is created, it can be parsed easily using the Date class and converted to any other timezone using enter image description here

  • Related