如何创建一个函数来计算/求和小时和分钟
我想创建一个函数来计算/求和时间。 例如,我每周工作 40 小时,我想将我每天工作的所有小时和分钟相加,以找出我工作了多少小时。 我尝试使用 Moment.js 但没有成功:/
var fTime = document.querySelector('input[id="fromTime"]').value;
var tTime = document.querySelector('input[id="toTime"]').value;
var btn = document.querySelector(".btn");
var sum = document.querySelector(".sum");
var reset = document.querySelector(".reset");
btn.onclick = sumTimes;
function sumTimes(){
sum.innerHTML = "You Worked " + tTime + " hours";
reset.style.opacity = 1;
}
reset.onclick = (e)=>sum.innerHTML = "00:00"
div.hours{
width:300px;
background-color:red;
heigth:70px;
text-align:center;
color:white;
display:flex;
flex-direction:column;
align-items:center;
}
.hours > *{
margin:3px;
}
.reset{
opacity:0;
}
<div class="wrapper">
<div class="hours">
<h2>Your Time</h2>
<label for="fromTime">From: </label>
<input id="fromTime" type="time" name="fromTime" value="13:30">
<label for="toTime">To: </label>
<input id="toTime" type="time" name="toTime" value="14:30">
<button class="btn">Enter</button>
<div class="sum">00:00</div>
<button class="reset">Reset</button>
</div>
</div>
I would like to create a function to calculate/sum the time.
For Example I work 40 hours per week and I want to sum all hours and minutes of each Day which I worked to find out how many hours did I work.
I tried with Moment.js but no luck :/
var fTime = document.querySelector('input[id="fromTime"]').value;
var tTime = document.querySelector('input[id="toTime"]').value;
var btn = document.querySelector(".btn");
var sum = document.querySelector(".sum");
var reset = document.querySelector(".reset");
btn.onclick = sumTimes;
function sumTimes(){
sum.innerHTML = "You Worked " + tTime + " hours";
reset.style.opacity = 1;
}
reset.onclick = (e)=>sum.innerHTML = "00:00"
div.hours{
width:300px;
background-color:red;
heigth:70px;
text-align:center;
color:white;
display:flex;
flex-direction:column;
align-items:center;
}
.hours > *{
margin:3px;
}
.reset{
opacity:0;
}
<div class="wrapper">
<div class="hours">
<h2>Your Time</h2>
<label for="fromTime">From: </label>
<input id="fromTime" type="time" name="fromTime" value="13:30">
<label for="toTime">To: </label>
<input id="toTime" type="time" name="toTime" value="14:30">
<button class="btn">Enter</button>
<div class="sum">00:00</div>
<button class="reset">Reset</button>
</div>
</div>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我们可以通过将起始时间和终止时间分解为小时和分钟部分来计算工作的
totalMinutes
。一旦我们获得了总工作分钟数,我们就可以使用
minutesToHoursAndMinutes()
函数将其格式化为小时和分钟。如果我们输入的“至:”时间早于“从:”时间,我们将显示错误,而不是尝试显示负时间。
我添加了一个
getTotalMinutesWorked()
函数,该函数将计算用户输入的总工作分钟数。We can calculate the
totalMinutes
worked by splitting the from time and to time into their hour and minute components.Once we have the total minutes worked, we can format this as hours and minutes using a
minutesToHoursAndMinutes()
function.If we enter a To: time that is earlier than the From: time, we'll show an error rather than trying to show a negative time.
I've added a
getTotalMinutesWorked()
function that will calculate the total minutes worked from the user inputs.您可以使用 moment.js 来完成此操作,
请查看以下文章:
使用 moment js 计算两个日期的差异
或纯 JavaScript 代码
//sumTimes();
这将产生毫秒级的差异。
You can do it by using moment.js
look the following article:
use moment js for difference two dates
or pure javascript code
//sumTimes();
this will give difference by miliseconds.