1

My local time zone is IST , i tried to define timezone inside Date object like

const usaRegionTime = new Date(inputDate.toString()).toLocaleString(
        'en-US',
        {
          timeZone: 'America/New_York',
        }
      );
      const estDate = new Date(usaRegionTime);
console.log(estDate.getTime());// Uses IST(local time Zone) instead of EST(Specifed time zone)

Example:

My IST time is 7:15 PM and i use the Date object with EST timeZone i get correct EST as 9:45 AM but when i try est.getTime() UTC is calculated as 7:15 pm instead of 1:45 pm . Its taking 9:45 am as IST(Local time Zone) before converting to UTC(Epoch).

Problem is its Taking Browser local timeZone(IST) instead of TimeZone(EST) i gave as param to Date object to get Epoch value . Is there a way to override the Local time Zone with Specified TimeZone while getting Epoch value.

  • 1
    What you're trying to do is flawed, see [this answer](https://stackoverflow.com/a/15171030/257182). This is likely a duplicate of [Converting timezones in javascript resulted in very strange results, am I doing something wrong or is it a bug?](https://stackoverflow.com/questions/61901653/converting-timezones-in-javascript-resulted-in-very-strange-results-am-i-doing) – RobG May 21 '20 at 20:17

2 Answers2

1

Is there a way to override the Local time Zone with Specified TimeZone while getting Epoch value.

No. You can generate a timestamp for any supported IANA representative location, but you can't "set a timezone" for the Date instance itself.

ECMAScript Date objects are just a time value that is an offset in milliseconds from 1 Jan 1970 UTC. They have no timezone and can only work in either the local timezone or UTC, that's it. When parsing a timestamp for a particular timezone, the result is a UTC time value. That is the only thing the Date instance remembers so it doesn't matter how you generate a Date, the time value is always effectively UTC.

The plain get and set methods like getYear, getMintues, etc. and the toString methods use the UTC time value and host settings to generate local date and time values. The UTC get and set methods like getUTCYear, getUTCMintues, etc. and toISOString, toUTCString etc. return UTC values. So you can work in either local or UTC, working in some other timezone or location requires you to do the work (which is quite complex and likely a library is a better option).

You can get a timestamp for any supported IANA representative location using toLocaleString with the timeZone option, however that only affects the generated string, it doesn't change the underlying UTC time value.

You can't set a timezone or location for a Date object as it doesn't have any property on which to set it, it's just a (UTC) time value, that's it.

Also, the built–in parser is not required to parse the output from toLocaleString, primarily because the user can configure the output to a huge number of different formats that may not include any date or time values (e.g. you can get just the timezone or offset). So depending on parsing such values is fundamentally flawed.

RobG
  • 142,382
  • 31
  • 172
  • 209
0
var date= new Date().valueOf(); //gives UTC timestamp in millisecond
const usaRegionTime = new Date(date).toLocaleString(
        'en-US',
        {
          timeZone: 'America/New_York',
        }
      );
  const estDate = new Date(usaRegionTime).toString();
console.log(estDate);
Gayathri
  • 1,776
  • 5
  • 23
  • 50
  • now if we use getTime method of estDate , It will take local timezone for Epoch conversion but i want to override the local time zone will epoch conversion – Mithun abisheg May 21 '20 at 14:56
  • https://stackoverflow.com/questions/36206260/how-to-set-date-always-to-eastern-time-regardless-of-users-time-zone – Gayathri May 21 '20 at 15:02
  • Thanks but this does not take in daylight saving into account and so offset cannot be constant value it changes in a cycle – Mithun abisheg May 21 '20 at 15:47
  • This only works if the host offset settings match the desired location. – RobG May 21 '20 at 20:20