简体   繁体   English

Moment JavaScript-获取两个日期之间的差异

[英]Moment JavaScript - get differences between two date

I'm trying to get date differences between the today date and a destination date in moment.js, here what I have do: 我试图在moment.js中获取今天的日期和目标日期之间的日期差,这是我所做的:

var now = moment().format('DD/MM/YYYY HH:mm:ss');
var then = moment("15/09/2017 14:20:30").format('DD/MM/YYYY HH:mm:ss');
//here i try the difference
var diffTime = moment.utc(moment(now).diff(moment(then)));

console.log("differenza per countdown "+diffTime);

The console.log return me "Invalid date" but if I use now as "then" it return me 0 (which is correct), how can I create a properly formatting data? console.log向我返回“无效日期”,但是如果我现在将其用作“ then”,它将返回0(这是正确的),我如何创建正确格式的数据?

Why are you formatting the moment to a string just to parse it again thereafter? 您为何将时刻格式化为字符串以便随后再次解析? There's no point in doing this. 这样做是没有意义的。

Use moment.duration on the difference: 在区别上使用moment.duration:

 var now = moment(); var then = moment("15/09/2017 14:20:30", "DD/MM/YYYY HH:mm:ss"); //here i try the difference var diffTime = moment.duration(then.diff(now)); console.log("differenza per countdown "+diffTime); console.log("humanized: " + diffTime.humanize(true)); console.log(diffTime.days() + "/" + diffTime.months() + "/" + diffTime.years() + " " + diffTime.hours() + ":" + diffTime.minutes() + ":" + diffTime.seconds()); 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.18.1/moment-with-locales.min.js"></script> 

Your date format is not a standard one, in that case specify it explicitly as second parameter or parsing will fail in different browsers. 您的日期格式不是标准格式,在这种情况下,应将其明确指定为第二个参数,否则解析将在不同的浏览器中失败。

moment("15/09/2017 14:20:30", 'DD/MM/YYYY HH:mm:ss')

Ans yes, there is no value in converting to string and parsing it back to date unless you have some other logic in between. 回答是的,除非您之间有其他逻辑,否则转换为字符串并将其解析为最新值没有任何价值。

 window.onload = function(){ var now = moment().format('DD/MM/YYYY HH:mm:ss'); var then = moment("15/09/2017 14:20:30", 'DD/MM/YYYY HH:mm:ss').format('DD/MM/YYYY HH:mm:ss'); //here i try the difference var diffTime = moment.utc(moment(now, 'DD/MM/YYYY HH:mm:ss').diff(moment(then, 'DD/MM/YYYY HH:mm:ss'))); console.log("differenza per countdown "+diffTime); } 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.18.1/moment-with-locales.min.js"></script> 

For me the result is NaN but date parsing can differ with browsers. 对我来说,结果是NaN但日期解析可能因浏览器而异。

 window.onload = function(){ var now = moment().format('DD/MM/YYYY HH:mm:ss'); var then = moment("15/09/2017 14:20:30").format('DD/MM/YYYY HH:mm:ss'); //here i try the difference var diffTime = moment.utc(moment(now).diff(moment(then))); console.log("differenza per countdown "+diffTime); } 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.18.1/moment-with-locales.min.js"></script> 

您还可以使用Date.parse()将日期转换为毫秒,然后找出差异。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM