Javascript 在momentjs中将分钟添加到日期时间
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/42031906/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Adding minutes to datetime in momentjs
提问by CsharpBeginner
I need to add the variable secondsToMinutesto the startdate.
我需要将变量添加secondsToMinutes到startdate.
secondsToMinutesis "3:20"
startDate= "2:00 PM"
endDateshould equal "2:03:20 PM".
I've tried a number of ways and get errors each and every time.
secondsToMinutes是“3:20”
startDate=“2:00 PM”
endDate应该等于“2:03:20 PM”。我尝试了多种方法,但每次都会出错。
var startdate = data.StartTime;
startdate = moment(startdate).format('LTS');
var secondsToMinutes = readableDuration(self.runlength());//='3:20';
var seconds = secondsToMinutes.split(':')[1];
var minutes = secondsToMinutes.split(':')[0];
var date = moment(startdate)
.add(seconds, 'seconds')
.add(minutes, 'minutes')
.format('LTS');
Date shows up as invalid date.
日期显示为无效日期。
回答by Muralidharan
moment().format("LTS")returns a string value in hh:mm:ss AM/PMformat.
When you create a moment object using a string that is not in standard format, you should pass the input format as second parameter to moment constructor.
moment().format("LTS")以hh:mm:ss AM/PM格式返回字符串值。当您使用非标准格式的字符串创建 moment 对象时,您应该将输入格式作为第二个参数传递给 moment 构造函数。
For eg: Jan 1, 2017in string 01012017is not a standard representation. But if you need a moment object out of it, using moment("01012017")will give "Invalid Date" response when formatting. Instead, use moment("01012017","DDMMYYYY")
例如:Jan 1, 2017in string01012017不是标准表示。但是如果你需要一个 moment 对象,使用它moment("01012017")会在格式化时给出“无效日期”响应。相反,使用moment("01012017","DDMMYYYY")
var d = moment("01012017")
d.toISOString() => "Invalid date"
var d = moment("01012017", "DDMMYYYY")
d.toISOString() => "2016-12-31T18:30:00.000Z"
In your code, when creating 'date' variable pass "hh:mm:ss A" as second parameter in the moment constructor as mentioned below .
在您的代码中,创建 'date' 变量时,将“hh:mm:ss A”作为构造函数中的第二个参数传递,如下所述。
var date = moment(startdate, "hh:mm:ss A")
.add(seconds, 'seconds')
.add(minutes, 'minutes')
.format('LTS');
回答by The Dembinski
Moment has really good documentation. I would check it out: http://momentjs.com/docs/
Moment 有非常好的文档。我会检查一下:http: //momentjs.com/docs/
But to address your question more directly, you coulddo something like:
但要更直接地解决您的问题,您可以执行以下操作:
var secondsToMinutes = '3:20';
var seconds = secondsToMinutes.split(':')[1];
var minutes = secondsToMinutes.split(':')[0];
var momentInTime = moment(...)
.add(seconds,'seconds')
.add(minutes,'minutes')
.format('LT');
You should use the actual handlers to the best of your ability. There are some cool things you can do with durations now, but this is more succinct.
您应该尽最大努力使用实际的处理程序。现在你可以用持续时间做一些很酷的事情,但这更简洁。
Edit:
编辑:
As mentioned in a different answer:
正如在不同的答案中提到的:
moment('2:00:00 PM', 'h:mm:ss A')
Is necessary if you're handling thatformat. Regardless - adding/subtracting minutes/hours to a moment object is trivial. Passing invalid strings to a moment object is a different issue in-and-of itself. ;)
如果您正在处理该格式,则这是必要的。无论如何 - 为时刻对象添加/减去分钟/小时是微不足道的。将无效字符串传递给 moment 对象本身就是一个不同的问题。;)
回答by ischenkodv
In the last moment()call you're trying to parse the value '2:00:00 PM'. According to the docs you have to use String+Format call in this case:
在最后一次moment()调用中,您试图解析值“2:00:00 PM”。根据文档,在这种情况下您必须使用 String+Format 调用:
For consistent results parsing anything other than ISO 8601 strings, you should use String + Format.
对于解析除 ISO 8601 字符串以外的任何内容的一致结果,您应该使用字符串 + 格式。
When you do:
当你这样做时:
var date = moment('2:00:00 PM')
.add(30, 'seconds')
.add(2, 'minutes')
.format('LTS');
The datewill be an Invalid date. But if you pass format h:mm:ss Aas a second parameter:
该date会是一个Invalid date。但是,如果您将格式h:mm:ss A作为第二个参数传递:
var date = moment('2:00:00 PM', 'h:mm:ss A')
.add(30, 'seconds')
.add(2, 'minutes')
.format('LTS');
... the result is 2:02:30 PM.
……结果是2:02:30 PM。

