简体   繁体   English

随时间流逝改变日子和时间

[英]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.

相关问题 Javascript:如何将毫秒显示为天:小时:分钟? - Javascript: How to show time in milliseconds as days: hours: min? 怎么把小时转换成几天的小时和分钟? - How to convert hours to days hours and minutes? 如何将HighChart xAxis的时间从军事时间改为标准时间? - How to change time in hours on HighChart xAxis from Military to Standard time? 如何使用更改事件更改启用日期时间选择器的小时数? - How to change Date Time Picker enabled hours using on change event? 特定时间后从几天切换到几小时 - Switch from days to hours after a certain time 按分钟,按小时,按天等格式化时间 - Formating time by minutes, by hours, by days etc 我如何将我的正常运行时间命令的格式更改为当前时间,我想将其更改为几天 - How do I change the format for my uptime command its currently hours I would like to change it to days 如何编写一个 function 可以使用 date-fns 正确地将小时和天添加到目标时区? - How to write a function that can add hours and days correctly to the target time zone with date-fns? 将时间插件更改为24小时 - change time plugin in to 24 hours 如何计算到达某一天的剩余时间(以天、小时、分钟和秒为单位)? - How to calculate the time left in days, hours, minutes, and seconds left to reach certain day?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM