How to calculate east time using a Javascript Date object?

I am working on a personal project involving Javascript, and as part of this project I want to take the current date (including time) and display it accordingly. Nothing wrong? Well, the thing is, I want to return the time and date to Eastern Daylight TIme, no matter where the IP is in the world.

If this is not possible, what alternative methods do you suggest? Does php have this functionality? I could write a simple php script that takes a date and converts it, but I want to keep this in JS, if at all possible.

I am trying to think of a better way to do this, but I would appreciate any help you could offer.

Thank!

+3
source share
2 answers

JavaScript native Dateobjects know only two time zones, UTC and a user time zone (and even then the amount of information that you can extract from a locale’s time zone is limited). You can work in UTC and subtract 4 hours to get EDT, but do you really want EDT, not EST?

If you want to do time zone conversions between arbitrary regions in PHP, you need to drag and drop a large library with its own time zone information, such as TimezoneJS .

It might be better to save the JavaScript content in UTC and let the PHP side worry about formatting it for a specific language / time zone, using, for example, the time zone material from Date / Time .

+1

, :

function calcTime(offset) {

    // create Date object for current location
    d = new Date();

    // convert to msec
    // add local time zone offset 
    // get UTC time in msec
    utc = d.getTime() + (d.getTimezoneOffset() * 60000);

    return new Date(utc + (3600000*offset));

}

, , , UTC, , .

+5

Source: https://habr.com/ru/post/1773084/


All Articles