Ewan Valentine
Ewan Valentine

Reputation: 3941

Node.js server time instead of local device time

I've been working on an app which is working across 500 iPads and whilst testing across the devices I noticed the times were incorrect across almost all of them, because it was using the time from the device and not the server. I'm new new Date();

Is there a way I can use the server time instead? I'm fairly new to Node, so please forgive me if it's something obvious!

Update:

Yes I'm trying to display the exact server time on the devices, not the time on the local device. I have looked around, but couldn't find any solid answers. Again, apologies if I have been vague, but as I said, I'm fairly new to Node.

Upvotes: 1

Views: 11555

Answers (2)

radhe_shyam
radhe_shyam

Reputation: 159

This route(in Node.js) helped for me:

app.get('/', (req, res) => {
    res.send('<script>var r=new Date().valueOf() + ( ' + (new Date().getTimezoneOffset()) +
        ' - (new Date().getTimezoneOffset()) ) * -60000;' +
        'setInterval(()=>{document.body.innerHTML = (new Date(r+=1000)).toLocaleString("en",{weekday:"long", month:"long", day:"numeric", year:"numeric", hour:"numeric", minute:"numeric", second:"numeric", hour12:false})},1000);' +
        '</script>');
});

Upvotes: 2

Roysh
Roysh

Reputation: 1550

Try using moment

npm install moment --save

Full documentation

http://momentjs.com/docs

Check their documentation about getting local time:

http://momentjs.com/docs/#/manipulating/local/

Upvotes: 2

Related Questions