moment.js 连接日期和时间 [英] moment.js concatenate date and time
本文介绍了moment.js 连接日期和时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在一个表单中有四个字段,一些包含初始日期和结束日期 (dd/mm/yyyy),其他包含开始时间和结束时间 (hh: ss).
I have four fields in a form, some containing an initial date and the end date (dd / mm / yyyy) and the others contain the start time and the end time (hh: ss).
我用来通过moment.js获取日期和时间的这些字段的值,例如:
The value of these fields I use to get the date and time with moment.js such as this:
initialdate = moment( $('input#start_date').val(), 'DD/MM/YYYY' );
start_time = moment( $('input#start_time').val(), 'HH:mm');
enddate = moment( $('input#enddate').val(), 'DD/MM/YYYY' );
end_time = moment( $('input#end_time').val(), 'HH:mm');
我打算然后获得两个日期之间的秒差,连接开始日期和时间以及结束日期和时间.我曾尝试这样做,但无济于事:
What I intend is to then get the difference in seconds between the two dates, concatenating the starting date and time and the ending date and time. I have tried to do this, but to no avail:
start = initialdate + start_time;
end = enddate + end_time;
tracker = moment.duration( end.diff(start) ).asSeconds();
推荐答案
失败是尝试连接值,测试如下:
The fail is trying on concatenate the values, test with something like this:
let initialdate = '2016-10-01';
let start_time = '19:04:10';
let enddate = '2016-10-01';
let end_time = '19:04:20';
let datetimeA = moment(initialdate + " " + start_time);
let datetimeB = moment(enddate + " " + end_time);
console.log(datetimeA.format());
console.log(datetimeB.format());
let datetimeC = datetimeB.diff(datetimeA, 'seconds');
console.log(datetimeC);
这篇关于moment.js 连接日期和时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文