momentjs不会添加分钟

时间:2014-05-12 12:30:08

标签: javascript momentjs

我有以下格式的日期和时间值“12-may-2014”和“16:05”

我正在尝试将这两个值转换为像这样的时刻对象

var time = moment(originalDate + ', ' + orignalTime);

这似乎返回了一个时刻对象所以我这样做

var newTime = moment(time).add('h', 3);
console.log(newTime);

这会记录预期时间“19:05”

但如果我这样做

var newTime = moment(time).add('m', 30);
console.log(newTime);

而不是获得“16.30”我只是得到原始时间“16.05”

编辑:如果我替换

,所选答案将解决我的问题
console.log(newTime.format("dddd, MMMM Do YYYY, h:mm:ss a")); 

console.log(newTime.format("HH:MM"));

我得到了原始问题。添加小时会显示正确的结果,但添加分钟会显示原始时间

console.log(newTime.format("hh:mm"));

按预期工作

2 个答案:

答案 0 :(得分:7)

似乎对我来说很好。您确定不会混淆2 time个变量吗?

这是working plunker

<强> JS:

var timevalue = "16:05";
var datevalue = "12-may-2014";
var time = moment(datevalue + ', ' + timevalue);

var newTime = moment(time).add('m', 30);
console.log(newTime.format("dddd, MMMM Do YYYY, h:mm:ss a"));

答案 1 :(得分:0)

对于他们来说,谁能理解moment.js对象发生了什么

调用momentValue.add('minute', amount)函数时,它返回Moment对象,但不是克隆对象,它是具有更改值的同一参考对象。 (3至7行之间)

因此,如果您不想更改原始对象,则应在应用更改之前使用.clone函数。 (9到14行之间)

moment using clone