【JavaScript】Obtain the Date by fixing it in Japan Time (JST)

It does not depend on the browser or server, and in the case of a PC where JST cannot be selected, it is fixed to Japan time.

目次

Get the Date in Japan Time (JST)

For PCs where JST cannot be selected, even if timeZone is specified, it will be returned in UTC. Therefore, use getTimezoneOffset to obtain it. This returns the time zone difference in minutes from the current locale to UTC (Coordinated Universal Time).

For example, Japan time is 9 hours ahead, so it is 9*60(540) minutes ahead, so -540 is returned. Returns 0 in UTC. Use this difference to obtain Japan time as follows.

new Date(Date.now() + ((new Date().getTimezoneOffset() + (540)) * 60 * 1000));

-540 is returned when it is Japan time, so just do new Date() and get Japan time. Since 0 is returned when it is UTC, I add it as 540 minutes late and get Japan time.

よかったらシェアしてね!
目次