简体   繁体   English

moment.js 错误,有时会忽略这一天

[英]moment.js error, sometimes ignores the day

I have a problem with moment.js and it's that I have two dates (initial and final) when I initialize the dates, the initial is fine but the final date is not, moment.js ignores the day part of the date ,I set "2020-10-05T09:00" but in the console I get "2020-10T17:15Z", as you can see it doesn't have the day part.我有一个 moment.js 的问题,当我初始化日期时,我有两个日期(初始和最终),初始很好但最终日期不是,moment.js 忽略日期的日期部分,我设置“2020-10-05T09:00”但在控制台中我得到“2020-10T17:15Z”,如您所见,它没有白天部分。

Here is my code这是我的代码

const ini = moment("2020-10-01T09:00");
const fin = moment("2020-10-05T09:00");
var diff = fin.diff(ini, 'seconds');
console.log(fin);

The console prints:控制台打印:

{_isAMomentObject: true, _i: "2020-10T09:00Z", _isUTC: false, _pf: {…}, _locale: x, …} {_isAMomentObject: true, _i: "2020-10T09:00Z", _isUTC: false, _pf: {…}, _locale: x, …}

Pass the format of your date as the second parameter to create a momentjs object based on your date .将日期格式作为第二个参数传递,以根据您的日期创建一个 momentjs 对象

Also, after calculating the fin.diff(ini, 'seconds') , you'll need to log diff to get the number of seconds;此外,在计算fin.diff(ini, 'seconds') ,您需要记录diff以获取秒数;

 const ini = moment("2020-10-01T09:00", 'YYYY-MM-DDThh:mm'); const fin = moment("2020-10-05T09:00", 'YYYY-MM-DDThh:mm'); var diff = fin.diff(ini, 'seconds'); console.log(diff); // output: 345600
 <script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.24.0/moment.min.js"></script>

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

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