简体   繁体   English

如何使用angular12根据2个日期计算天数

[英]how to make calculation of days based on 2 dates using angular12

i am using angular12, here calculation of days based on 2 dates works fine with chrome, but the same thing fails in firefox.我正在使用 angular12,这里基于 2 个日期的天数计算在 chrome 上工作正常,但同样的事情在 firefox 中失败了。

TS: Using Moment, gives invalid date error under firefox: TS:使用 Moment,在 firefox 下给出无效日期错误:

 getDaysCount(firstDate: any, secondDate: any) {
    let first = moment(firstDate, 'MM-DD-YYYY');
    let second = moment(secondDate, 'MM-DD-YYYY');
    return second.diff(first, 'days');
  }

console.log(this.getDaysCount('11-12-2022', '11-14-2022)); console.log(this.getDaysCount('11-12-2022', '11-14-2022));

var Difference_In_Days = Math.ceil(Math.abs(secondDate - firstDate) / (1000 * 60 * 60 * 24));

this gives NAN in firefox but gives 2 in chrome.这在 firefox 中给出 NAN,但在 chrome 中给出 2。

Take a look at this answer .看看这个答案 Firefox requires the date in yyyy-mm-dd format. Firefox 需要yyyy-mm-dd格式的日期。 If you make the change, it works in both Firefox and Chrome.如果您进行更改,它在 Firefox 和 Chrome 中都有效。

FYI, you cannot use Math.ceil on strings, you need to convert them to milliseconds first.仅供参考,您不能在字符串上使用Math.ceil ,您需要先将它们转换为毫秒。

getDaysCount(firstDate: any, secondDate: any) {
    const firtDateMs = (new Date(firstDate)).getTime();
    const secondDateMs = (new Date(secondDate)).getTime();
    console.log('First date: ' + firstDate, 'In ms:' + firtDateMs);
    console.log('Second date: ' + firstDate, 'In ms:' + secondDateMs);
    const Difference_In_Days = Math.ceil(Math.abs(firtDateMs - secondDateMs) / (1000 * 60 * 60 * 24));
    console.log("Difference_In_Days: ", Difference_In_Days);
  }

To include negative numbers in the result, remove Math.abs from the function.要在结果中包含负数,请从 function 中删除Math.abs

const Difference_In_Days = Math.ceil((firtDateMs - secondDateMs) / (1000 * 60 * 60 * 24));

Firefox console: Firefox 控制台: 在此处输入图像描述

Chrome console:铬控制台:

在此处输入图像描述

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

相关问题 如何根据日期计算天数 - How to calculate no of days based on dates 错误:无法读取未定义的属性(读取“管道”)Angular12 - Error: Cannot read properties of undefined (reading 'pipe') Angular12 Angular12 - 将函数传递给组件并处理结果 - Angular12 - Passing function to the component and handling result 使用 angular 的天数中的两个日期之间的差异 - Difference between two dates in Days using angular 为什么在循环空数组后在 angular12 中出现“无法编译错误”? - Why do I get 'Failed to compile error' in angular12 after looping an empty array? (Angular12) 应用过滤器时 UI 不会更新(每次都显示相同数量的表行) - (Angular12) UI is not updating when a filter is applied (shows the same number of table rows every time) 在回调 function Angular12 和电容器 V-3 中访问 Class 成员 - Access Class Members within a call back function Angular12 and Capacitor V-3 如何计算日期和小时之间的小时数 - How would I make the hour calculation between to dates and hours 如何根据第一天的第二天禁用第二个日期时间选择器中的日期 - How to disable dates in second datetimepicker based on next days in the first one 带有日期和日期的角度甘特图 - Angular Gantt chart with days and dates
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM