[英]change days and hours by how time flies
I am getting the elapsed time in minutes, hours and days, between two dates, a past date and the current one, I already get this data, but I want this data to change as the minutes, days and hours increase.我以分钟、小时和天为单位获得经过的时间,在两个日期之间,一个过去的日期和当前的日期,我已经得到了这个数据,但我希望这个数据随着分钟、天和小时的增加而改变。 For example, when I get to 60 minutes, the time changes to 1 hour and the minutes go to 0, when 24 hours go by, these hours change to a day and the hours go back to 0, and so on, the data I get keeps increasing, how can I do this?
例如,当我达到60分钟时,时间会更改为1小时和分钟Z34D1FB2E514B8576FAB1A75A89A6BZ,为0,当24小时Z34D1FB2E51FB2E514B8576FB8576FAB1A75A6BZ乘以每天的时间和时间Z31111111444444444444444444444444444444444444444444444444444444岁。得到不断增加,我该怎么做?
const calculateDate = () => {
const date = new Date('Sun Sep 01 2022 01:32:06 GMT-0500');
const currentDate = new Date();
const minutes= Math.floor((currentDate.getTime() - date.getTime()) / 1000 / 60);
const hours= Math.floor((currentDate.getTime() - date.getTime()) / 1000 / (3600));
const days= Math.floor((currentDate.getTime() - date.getTime()) / (1000*60*60*24));
}
With this, get the minutes, hours and days, but how would you update so that when you reach 60 minutes it goes to one hour and 24 hours to one day?有了这个,得到分钟、小时和天,但是你将如何更新,以便当你达到 60 分钟时它会变成一小时和 24 小时到一天?
The JavaScript Date
object has built in functions for what you want to do. JavaScript
Date
object 内置了您想做的功能。
var now = new Date()
var h = now.getHours()
var m = now.getMinutes()
var s = now.getSeconds()
The new Date created in above example is set to the time it was created.上面示例中创建的新日期设置为它的创建时间。
You can get the current time using the Date object itself:您可以使用 Date object 本身获取当前时间:
var current = Date()
With your method you always see the full duration just in a different unit.使用您的方法,您总是可以在不同的单位中看到完整的持续时间。
You have to use the modulo operator to get only the "missing part" (the remainder of the division) to the next unit:您必须使用模运算符仅将“缺失的部分”(除法的其余部分)获取到下一个单元:
const date = new Date('Sun Sep 01 2022 01:32:06 GMT-0500');
const currentDate = new Date();
const dateDiff = (currentDate.getTime() - date.getTime()) / 1000;
const seconds = Math.floor(dateDiff) % 60;
const minutes = Math.floor(dateDiff / 60) % 60;
const hours = Math.floor(dateDiff / (60 * 60)) % 24;
const days = Math.floor(dateDiff / (60 * 60 * 24));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.