Javascript 如何在Moment.js中设置默认偏移量?
我目前正在使用Moment.js操纵日期和时间 如果用户手动更改其设备上的时间,我会尽量避免出现问题 所以我得到了服务器时间,检查了浏览器时间的偏移量Javascript 如何在Moment.js中设置默认偏移量?,javascript,momentjs,Javascript,Momentjs,我目前正在使用Moment.js操纵日期和时间 如果用户手动更改其设备上的时间,我会尽量避免出现问题 所以我得到了服务器时间,检查了浏览器时间的偏移量 var offset = moment(browserTime).diff(moment(serverTime)); 但我不想存储该偏移量,每次创建新时刻时都必须应用该偏移量,如下所示: var foo = moment().add('milliseconds', offset); 有没有办法在力矩中设置偏移量,这样每个新力矩都会应用偏移量?
var offset = moment(browserTime).diff(moment(serverTime));
但我不想存储该偏移量,每次创建新时刻时都必须应用该偏移量,如下所示:
var foo = moment().add('milliseconds', offset);
有没有办法在力矩中设置偏移量,这样每个新力矩都会应用偏移量?比如:
moment.setOffset(offset);
var foo = moment();
var bar = moment(new Date()).add('milliseconds', offset);
if (foo.isSame(bar)) {
// success
}
var offsetMoment = (function(){
var globalOffset = moment.duration(); // Defaults to no offset
var offsetMoments = []; // Stores all moments that have a global offset
var throwIfNotOffsetMoment = function(moment){ // Regular moment objects can't use offset methods
if(! moment.isOffsetMoment){
throw "- Moment is not an offsetMoment.";
}
};
// Sets an the globalOffset and applies it to all offsetMoments
// Offset is not relative to the current offset, example:
// now.setGlobalOffset(1,'day') //tommorrow
// now.setGlobalOffset(-1,'day') //yesterday, not today
// Same arguments accepted as moment.duration
moment.fn.setGlobalOffset = function(){
var offset = moment.duration.apply(this,arguments);
throwIfNotOffsetMoment(this);
for(i in offsetMoments){
offsetMoments[i].subtract(globalOffset).add(offset); // Subtract the old offset and add the new one
}
globalOffset = offset;
return this;
}
// Return a new moment object without the offset
moment.fn.baseTime = function(){
throwIfNotOffsetMoment(this);
return this.clone().subtract(globalOffset);
};
// Return a factory function to offsetMoment that creates offset moments
// Makes it work exactly like the momentjs's constructor
return function(){
offsetMoments.push(moment.apply(this,arguments));
offsetMoments[offsetMoments.length-1].isOffsetMoment = true;
offsetMoments[offsetMoments.length-1].add(globalOffset);
return offsetMoments[offsetMoments.length-1];
}
})();
// Usage
var offsetA = offsetMoment("2012-10-05");
offsetA.setGlobalOffset(5,'minutes'); // 2012-10-05 00:05
offsetA.baseTime(); // return new moment object = 2012-10-05 00:00
var offsetB = offsetMoment(moment()); // five minutes from now
如果没有,那么将该功能添加到momente.js(请注意)可能是一个好主意
谢谢
旁白:我知道这个问题与我的问题密切相关,但提出的答案并不能解决我的问题。我可能会做一些类似的事情:
moment.setOffset(offset);
var foo = moment();
var bar = moment(new Date()).add('milliseconds', offset);
if (foo.isSame(bar)) {
// success
}
var offsetMoment = (function(){
var globalOffset = moment.duration(); // Defaults to no offset
var offsetMoments = []; // Stores all moments that have a global offset
var throwIfNotOffsetMoment = function(moment){ // Regular moment objects can't use offset methods
if(! moment.isOffsetMoment){
throw "- Moment is not an offsetMoment.";
}
};
// Sets an the globalOffset and applies it to all offsetMoments
// Offset is not relative to the current offset, example:
// now.setGlobalOffset(1,'day') //tommorrow
// now.setGlobalOffset(-1,'day') //yesterday, not today
// Same arguments accepted as moment.duration
moment.fn.setGlobalOffset = function(){
var offset = moment.duration.apply(this,arguments);
throwIfNotOffsetMoment(this);
for(i in offsetMoments){
offsetMoments[i].subtract(globalOffset).add(offset); // Subtract the old offset and add the new one
}
globalOffset = offset;
return this;
}
// Return a new moment object without the offset
moment.fn.baseTime = function(){
throwIfNotOffsetMoment(this);
return this.clone().subtract(globalOffset);
};
// Return a factory function to offsetMoment that creates offset moments
// Makes it work exactly like the momentjs's constructor
return function(){
offsetMoments.push(moment.apply(this,arguments));
offsetMoments[offsetMoments.length-1].isOffsetMoment = true;
offsetMoments[offsetMoments.length-1].add(globalOffset);
return offsetMoments[offsetMoments.length-1];
}
})();
// Usage
var offsetA = offsetMoment("2012-10-05");
offsetA.setGlobalOffset(5,'minutes'); // 2012-10-05 00:05
offsetA.baseTime(); // return new moment object = 2012-10-05 00:00
var offsetB = offsetMoment(moment()); // five minutes from now
包含一些基本测试用例
可以通过扩展原型来完成类似的工作,但这已经足够好了
基本上,您是在offsetMoment
中包装moment.js
,并给它一些共享变量。然后扩展moment.fn
,以便轻松访问/操作共享变量
它仅限于一个全局偏移(但常规力矩在没有偏移的情况下仍然有效)
它根本不会影响moment的常规功能。而offsetMoment
对象应与moment
对象配合良好
应该很容易调整到精确的规格。那么,添加一个全局偏移?如果在您的网站上的其他地方使用了矩,而不需要偏移,会发生什么情况?IMO表示,能够获得一个可选配置的矩实例(或围绕它的包装器)将是一种更好的方法;类似于,
var myMoment=moment.withConfig({offset:…})代码>。如果偏移量存储在矩中,然后可以调用矩().applyOffset()代码>?那么剩下的时间就没问题了,而且不必全局公开偏移值。