First, let's talk about the code in your question.
let india_date = new Date()
You have named this variable india_date
, but the Date
object will only reflect India if the code is run on a computer set to India's time zone. If it is run on a computer with a different time zone, it will reflect that time zone instead. Keep in mind that internally, the Date
object only tracks a UTC based timestamp. The local time zone is applied when functions and properties that need local time are called - not when the Date
object is created.
add it's timezone offset value
let uts_ms = india_date.getTime() + india_date.getTimezoneOffset()
This approach is incorrect. getTime()
already returns a UTC based timestamp. You don't need to add your local offset. (also, the abbreviation is UTC, not UTS.)
Now add california's timezone offset value
let california_ms = utc_ms + getCaliforniaTimezoneOffsetMS()
Again, adding an offset is incorrect. Also, unlike India, California observes daylight saving time, so part of the year the offset will be 480
(UTC-8), and part of the year the offset will be 420
(UTC-7). Any function such as your getCaliforniatimezoneOffsetMS
would need to have the timestamp passed in as a parameter to be effective.
and finally the date object
let california_date: Date = new Date(california_ms)
When the Date
constructor is passed a numeric timestamp, it must be in terms of UTC. Passing it this california_ms
timestamp is actually just picking a different point in time. You can't change the Date
object's behavior to get it to use a different time zone just by adding or subtracting an offset. It will still use the local time zone of where it runs, for any function that requires a local time, such as .toString()
and others.
There is only one scenario where this sort of adjustment makes sense, which is a technique known as "epoch shifting". The timestamp is adjusted to shift the base epoch away from the normal 1970-01-01T00:00:00Z
, thus allowing one to take advantage of the Date
object's UTC functions (such as getUTCHours
and others). The catch is: once shifted, you can't ever use any of the local time functions on that Date
object, or pass it to anything else that expects the Date
object to be a normal one. Epoch shifting done right is what powers libraries like Moment.js. Here is another example of epoch shifting done correctly.
But in your example, you are shifting (twice in error) and then using the Date
object as if it were normal and not shifted. This can only lead to errors, evident by the time zone shown in toString
output, and will arise mathematically near any DST transitions of the local time zone and the intended target time zone. In general, you don't want to take this approach.
Instead, read my answer on How to initialize a JavaScript Date to a particular time zone. Your options are listed there. Thanks.