Convert UTC Epoch to Local Date Effectively
In a quest to transform UTC epochs to date objects, a common challenge arises when the assumption is made that the epoch provided is local. To address this issue, a more efficient approach can be employed.
The Solution
Instead of converting the UTC epoch to a UTC date and then adjusting it to the local time zone, the solution lies in setting the initial date to the epoch and adding UTC units. For instance, consider a UTC epoch in seconds, such as 1234567890:
var utcSeconds = 1234567890; var d = new Date(0); // Sets the date to the epoch d.setUTCSeconds(utcSeconds);
By utilizing this technique, the resulting date object, d, will accurately reflect the local time zone and provide the desired conversion from UTC epoch to local date.
The above is the detailed content of How to Convert a UTC Epoch to a Local Date Effectively?. For more information, please follow other related articles on the PHP Chinese website!