简体   繁体   English

如何在 JavaScript 中正确地减去分钟?

[英]How can I correctly subtract minutes from each other in JavaScript?

So I am trying to subtract two dates and ideally it should go like this:所以我试图减去两个日期,理想情况下它应该像这样 go :

23:00 - 22:11 = 0 hours 49 minutes 23:00 - 22:11 = 0 小时 49 分钟

But instead I keep getting 1 hours -11 minutes.但相反,我一直得到 1 小时 -11 分钟。

I do understand why this is happening, I am just not sure how could I fix it.我明白为什么会发生这种情况,我只是不确定如何解决它。

 let date1 = new Date("2022-08-05T23:00:00Z"); document.getElementById('test1').innerHTML = date1.toISOString().slice(0,-8)+"Z"; let date2 = new Date("2022-08-05T22:11:00Z"); document.getElementById('test2').innerHTML = date2.toISOString().slice(0,-8)+"Z"; let dateArrays = [date1, date2]; let allItems = document.querySelectorAll('.timePass'); allItems.forEach((item, index) =>{ let sum = (date1.getUTCHours() - dateArrays[index].getUTCHours()); let sum2 = (date1.getUTCMinutes() - dateArrays[index].getUTCMinutes()); item.innerHTML = `(${sum}h ${sum2}m ago)`; });
 <th> <div> <span id="test1"></span> <span class="timePass"></span> </div> </th> <th> <div> <span id="test2"></span> <span class="timePass"></span> </div> </th>

So, the minutes have a negative value, you need to subtract from the hour 1 and sum the minutes by 60所以,分钟有一个负值,你需要从小时中减去1并将分钟相加60

 let date1 = new Date("2022-08-05T23:00:00Z"); document.getElementById('test1').innerHTML = date1.toISOString().slice(0,-8)+"Z"; let date2 = new Date("2022-08-05T22:11:00Z"); document.getElementById('test2').innerHTML = date2.toISOString().slice(0,-8)+"Z"; let dateArrays = [date1, date2]; let allItems = document.querySelectorAll('.timePass'); allItems.forEach((item, index) =>{ let sum = (date1.getUTCHours() - dateArrays[index].getUTCHours()); let sum2 = (date1.getUTCMinutes() - dateArrays[index].getUTCMinutes()); if (sum2 < 0) { sum = sum - 1 sum2 = 60 + sum2 } item.innerHTML = `(${sum}h ${sum2}m ago)`; });
 <th> <div> <span id="test1"></span> <span class="timePass"></span> </div> </th> <th> <div> <span id="test2"></span> <span class="timePass"></span> </div> </th>

The built-in getTime() method converts your Date object value to the number of milliseconds since 1st January 1970.内置的 getTime() 方法将您的 Date object 值转换为自 1970 年 1 月 1 日以来的毫秒数。

You can use the returned millisecond values to subtract two different dates as shown below:您可以使用返回的毫秒值减去两个不同的日期,如下所示:

const dateOne = new Date("04/20/2021"); // 20th April 2021
const dateTwo = new Date("03/10/2021"); // 10th March 2021
const difference = dateOne.getTime() - dateTwo.getTime();
console.log(difference); // 3542400000

Then, you can use the returned difference value and convert it to months, days, minutes, or seconds depending on the measurement required by your project.然后,您可以使用返回的差值并将其转换为月、日、分钟或秒,具体取决于项目所需的度量。

To convert the difference from millisecond to second, just divide the difference by 1000:要将差异从毫秒转换为秒,只需将差异除以 1000:

const dateOne = new Date("04/20/2021"); // 20th April 2021
const dateTwo = new Date("03/10/2021"); // 10th March 2021
const difference = dateOne.getTime() - dateTwo.getTime();
console.log(difference / 1000); // 3542400

To convert the difference to minutes, you need to divide it by 1000 and then divide it again by 60 because one minute equals sixty seconds:要将差值转换为分钟,您需要将其除以 1000,然后再除以 60,因为一分钟等于六十秒:

console.log(difference / (1000 * 60)); // 59040

To find the difference in hours, you need to divide the difference further by 60 because one hour equals sixty minutes:要找出小时的差异,您需要将差异进一步除以 60,因为一小时等于六十分钟:

console.log(difference / (1000 * 60 * 60)); // 984

Finally, to calculate the difference in days, divide it by 24 because one day equals twenty-four hours:最后,要计算天数的差异,请将其除以 24,因为一天等于二十四小时:

console.log(difference / (1000 * 60 * 60 * 24)); // 41

This is the right way of doing it.这是正确的做法。 Source:资源:

Source: https://sebhastian.com/javascript-subtracting-dates/#:~:text=%20To%20subtract%20days%20from%20a%20JavaScript%20Date,the%20day%20of%20the%20Date%20object%20More%20来源: https://sebhastian.com/javascript-subtracting-dates/#:~:text=%20To%20subtract%20days%20from%20a%20JavaScript%20Date,the%20day%20of%20the%20Date%20object%20更多%20

You can use something like this:你可以使用这样的东西:

 let date1 = new Date("2022-08-05T23:00:00Z"); document.getElementById("test1").innerHTML = date1.toISOString().slice(0, -8) + "Z"; let date2 = new Date("2022-08-04T22:11:00Z"); document.getElementById("test2").innerHTML = date2.toISOString().slice(0, -8) + "Z"; let dateArrays = [date1, date2]; let allItems = document.querySelectorAll(".timePass"); const msToHoursMinutesSeconds = (ms) => { let seconds = Math.floor(ms / 1000); let minutes = Math.floor(seconds / 60); const hours = Math.floor(minutes / 60); seconds = seconds % 60; minutes = minutes % 60; return { hours, minutes, seconds }; } allItems.forEach((item, index) => { const diffMs = Math.abs(date1 - dateArrays[index]); const { hours, minutes, seconds } = msToHoursMinutesSeconds(diffMs); item.innerHTML = `(${hours}h ${minutes}m ${seconds}s ago)`; });
 <th> <div> <span id="test1"></span> <span class="timePass"></span> </div> </th> <th> <div> <span id="test2"></span> <span class="timePass"></span> </div> </th>

The idea is just to get the difference between the two dates, and then get the hours and minutes then.这个想法只是得到两个日期之间的差异,然后得到小时和分钟。

the Date object has some methods, I'm sure that methods will help you a lot. Date object 有一些方法,我相信这些方法会对你有很大帮助。

Try it: Method getMinutes() of the Date Object试试看: 日期 Object 的方法 getMinutes()

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

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