Home >Web Front-end >JS Tutorial >How to Convert UTC Epoch to Local Date?
Convert UTC Epoch to Local Date: A Comprehensive Solution
The challenge of converting a UTC epoch to a local date arises when the default behavior of the Date() constructor assumes local epoch. This can lead to inaccuracies in date representation.
To address this issue, a more robust approach can be employed:
Solution:
var d = new Date(0);
var utcSeconds = // Replace with your UTC epoch in seconds
d.setUTCSeconds(utcSeconds);
This approach ensures that the resulting date, stored in the "d" variable, represents the specified UTC epoch in the local time zone.
Example:
Consider a UTC epoch of 1234567890:
var utcSeconds = 1234567890; var d = new Date(0); d.setUTCSeconds(utcSeconds);
The "d" variable will now hold the local date equivalent to this UTC epoch: Fri Feb 13 2009 18:31:30 GMT-0500 (EST).
The above is the detailed content of How to Convert UTC Epoch to Local Date?. For more information, please follow other related articles on the PHP Chinese website!