使用moment.js向特定区域设置的日期添加天数

Adding days to locale specific dates using moment.js

本文关键字:日期 添加 设置 区域 js moment 使用      更新时间:2023-09-26

我试图使用moment-with-locale .min.js来添加(或减去)特定于locale的日期。我所拥有的是包含格式化为特定区域设置日期的文本框。当用户更新其中一个时,它会重置所有其他的,并添加指定的天数。然而,我似乎不能让moment.js基于en-US以外的任何东西来操作日期,所以日期是错误的。

我尝试了几个使用这个jsFiddle的变化

var d = new moment('11/08/2014').locale('en-GB'); //August 11, 2014
alert(d.locale()); //returns en-gb
d.add("days",1).format('L');
//d.add("days",1).locale('en-GB').format('L');
alert(d); //returns November 09, 2014 instead of 12/08/2014
//note, the format is incorrect as well, should be returning short format

我肯定我用错了,所以欢迎任何建议。

基于https://github.com/moment/moment/issues/665,您需要为解析器定义L标记。同时,在开始解析之前定义语言环境。最后的代码是

moment.locale('en-gb');
var d = new moment('11/08/2014', 'L'); //August 11, 2014
alert(d.locale()); //returns en-gb
d.add("days", 1);
alert(d.format('L')); //returns 12/8/2014
http://jsfiddle.net/omnbgk5s/2