5

I'm currently working on a MERN stack application and it's hosted through AWS EC2.

So when I try to get the current date/time using Node, I get the server's date and time.

How can I get the current time of a specific country/timezone on the server?

5
  • 1
    Is this server-side JavaScript (Node, or something)? Sep 17, 2017 at 2:46
  • you need to learn the time offset and apply it Sep 17, 2017 at 3:01
  • Set the server timezone to NZST.
    – robertklep
    Sep 17, 2017 at 6:39
  • 1
    You really should consider using an established solution for this, as there may be challenges you haven't considered like dealing with daylight savings or whatnot. Try moment.js timezone. Sep 17, 2017 at 7:33
  • So when I try to get the current date/time using Node, I get the server's date and time. This is almost always a bad idea. Set your server clock to UTC or you'll have a twice-a-year emergency reconciling when DST kicked in for each of your customers vs when it kicked in for your server. UTC on the server, clientside scripts will display dates in user-local time, convert them to UTC before sending to the server, everyone's happy, nobody gets hurt Sep 28, 2022 at 2:09

3 Answers 3

7

There are couple of options you have.

  1. Modify the EC2 instances timezone and send it to the client browser.
  2. Calculate the New Zealand time from web browser clients timezone.

If you follow the 1st option you can get the server UTC time using a HTTP request.

For the 2nd option, you need to calculate the time for New Zealand using JavaScript in client's browser.

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

// convert to milliseconds, add local time zone offset and get UTC time in milliseconds
var utcTime = date.getTime() + (date.getTimezoneOffset() * 60000);

// time offset for New Zealand is +12
var timeOffset = 12;

// create new Date object for a different timezone using supplied its GMT offset.
var NewZealandTime = new Date(utcTime + (3600000 * timeOffset));

Note: This won't reflect Daylight Saving Time.

3

Use a third party api to show time from a specific country. You can use api from worldtimeapi.org/. Make an ajax call, get the time of your desired location. You can use plain javascript or use any ajax library to do that. Here I'm including two methods: 1) plain javascript and 2) using axios

VANILLA JS

function getTime(url) {
    return new Promise((resolve, reject) => {
        const req = new XMLHttpRequest();
        req.open("GET", url);
        req.onload = () =>
            req.status === 200
                ? resolve(req.response)
                : reject(Error(req.statusText));
        req.onerror = (e) => reject(Error(`Network Error: ${e}`));
        req.send();
    });
}

Now Use this function to make the ajax call

let url = "http://worldtimeapi.org/api/timezone/Pacific/Auckland";

getTime(url)
    .then((response) => {
        let dateObj = JSON.parse(response);
        let dateTime = dateObj.datetime;
        console.log(dateObj);
        console.log(dateTime);
    })
    .catch((err) => {
        console.log(err);
    });

AXIOS

axios({
    url:"http://worldtimeapi.org/api/timezone/Pacific/Auckland",
    method: "get",
})
    .then((response) => {
        let dateObj = response.data;
        let dateTime = dateObj.datetime;
        console.log(dateObj);
        console.log(dateTime);
    })
    .catch((err) => {
        console.log(err);
    });

Hope that helps. Bear one thing in mind though, worldtimeapi.org/ is a third party service. If they choose to terminate their service, your code will break. Happy coding.

-1

You may try for this:

if server is Linux server then use,

var dateTime=new Date(linuxserveTime*1000);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.