Moment.js从服务器设置基准时间 [英] Moment.js set the base time from the server

查看:1086
本文介绍了Moment.js从服务器设置基准时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在阅读文档,似乎找不到从服务器而不是客户端设置日期的方法?

I have been reading the documentation, and cannot seem to find a method to set the date from the server as opposed to the client?

例如像这样的

moment().set('server time')

然后 moment()将根据服务器的时间返回Date对象,而不是客户端计算机的时间。

Then moment() would return the Date Object based on server's time as opposed to the time from the client's computer.

此功能是否存在,如果不是您将会使用的一些推荐方法?现在我正在轮询服务器以获取服务器的时间,但是您可以想像这不是很有效率。

Does this functionality exist, and if not what are some recommended methods you would use? Right now I am polling the server to get the server's time, but as you can imagine this is not very efficient.

我正在建立一个拍卖应用程序。所以服务器时间至关重要。例如,如果用户篡改了他们的时间设置或者手动配置了他们的时间,客户端可能会看到已经结束的拍卖,实际上还有几分钟的时间。所有的时间检查显然是在服务器上完成的,但这就是为什么我需要客户端和服务器同步。现在,您可以看到我正在轮询服务器以获得每隔几秒钟的最新时间。但是我需要的是设置moment()从我从服务器传递的变量中调用基准时间。所以每次我打电话的时候()都是从最初的时间开始的,而不是新的Date()。

I am building an auction application. So server time is critical. For example if the user has tampered with their time settings or their time has been manually configured, the client might see an auction that has already ended, when in fact it still has a few minutes left. All time checks are obviously done on the server, but that is why I need the client and server to be in sync. Now you can see I am polling the server to get the latest time every few seconds. However what I need is to set moment() to call the base time from the variable I pass from the server. So everytime I call moment() it bases the time from the time passed in initially instead of new Date().

app.getTime = ->
    setInterval ->
        $.get app.apiUrl + 'public/time', (time)  -> 
            app.now = moment(time).format('YYYY-MM-DDTHH:mm')
    , app.fetchTimeInterval

在我的应用程序的其他地方,我使用应用程序的服务器时间如此

Somewhere else in my application I use the app's server time like so

collection = new Auction.DealershipBasket [], query: "?$orderby=Ends&$filter=Ends lt datetime'#{app.now}'"

相反,我想调用以下内容,它返回服务器的时间,我只需要从上面的url获取请求来配置一次。

Instead I would like to call the following and it returns the time from server which I only need to configure once with a get request from the url above.

 now = moment().format('YYYY-MM-DDTHH:mm')


推荐答案

最好的方法是在当前时间请服务器一次,然后计算服务器时间和客户时间。一个函数可以通过使用当前客户端日期并应用服务器差异在任何阶段返回服务器时间。

The best way to do this would be to ask the server once for the current time and then compute the offset between the server time and the client time. A function can then return the server time at any stage by using the current client date and applying the server difference.

这里有一个例子:

var serverDate;
var serverOffset;
$.get('server/date/url', function(data){
   // server returns a json object with a date property.
   serverDate = data.date;
   serverOffset = moment(serverDate).diff(new Date());
});

function currentServerDate()
{
    return moment().add('milliseconds', serverOffset);
}

这篇关于Moment.js从服务器设置基准时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆