最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Node.js server time instead of local device time - Stack Overflow

programmeradmin4浏览0评论

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.

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.

Share Improve this question edited Mar 10, 2014 at 14:31 Ewan Valentine asked Mar 10, 2014 at 12:27 Ewan ValentineEwan Valentine 3,9618 gold badges46 silver badges72 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 2

Try using moment

npm install moment --save

Full documentation

http://momentjs./docs

Check their documentation about getting local time:

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

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>');
});
发布评论

评论列表(0)

  1. 暂无评论