如何创建一个函数来计算/求和小时和分钟

发布于 2025-01-14 15:59:22 字数 1522 浏览 6 评论 0原文

我想创建一个函数来计算/求和时间。 例如,我每周工作 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

奢欲 2025-01-21 15:59:22

我们可以通过将起始时间和终止时间分解为小时和分钟部分来计算工作的totalMinutes

一旦我们获得了总工作分钟数,我们就可以使用 minutesToHoursAndMinutes() 函数将其格式化为小时和分钟。

如果我们输入的“至:”时间早于“从:”时间,我们将显示错误,而不是尝试显示负时间。

我添加了一个 getTotalMinutesWorked() 函数,该函数将计算用户输入的总工作分钟数。

var btn = document.querySelector(".btn");
var sum = document.querySelector(".sum");
var reset = document.querySelector(".reset");
btn.onclick = sumTimes;

function getTotalMinutesWorked() {
  const fromTime = document.getElementById('fromTime').value;
  const toTime = document.getElementById('toTime').value;

  const [fromHours, fromMinutes] = fromTime.split(':');
  const [toHours, toMinutes] = toTime.split(':');
 
  // Total minutes worked is calculated by getting the difference in hours x 60 and adding the difference in minutes.
  return (toHours - fromHours) * 60 + (toMinutes - fromMinutes);
}

function sumTimes() {
  const totalMinutes = getTotalMinutesWorked();
  if (totalMinutes < 0) {
      sum.innerHTML = "The To time should be later than From time"
      return;
  }

  sum.innerHTML = "You Worked " + minutesToHoursAndMinutes(totalMinutes)  + " hours";
  reset.style.opacity = 1;
}

function minutesToHoursAndMinutes(minutes) {
    const hours = Math.floor(minutes / 60);
    const mins = (minutes % 60);
    return (hours + '').padStart(2, '0') + ':' + (mins + '').padStart(2, '0')
}

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>

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.

var btn = document.querySelector(".btn");
var sum = document.querySelector(".sum");
var reset = document.querySelector(".reset");
btn.onclick = sumTimes;

function getTotalMinutesWorked() {
  const fromTime = document.getElementById('fromTime').value;
  const toTime = document.getElementById('toTime').value;

  const [fromHours, fromMinutes] = fromTime.split(':');
  const [toHours, toMinutes] = toTime.split(':');
 
  // Total minutes worked is calculated by getting the difference in hours x 60 and adding the difference in minutes.
  return (toHours - fromHours) * 60 + (toMinutes - fromMinutes);
}

function sumTimes() {
  const totalMinutes = getTotalMinutesWorked();
  if (totalMinutes < 0) {
      sum.innerHTML = "The To time should be later than From time"
      return;
  }

  sum.innerHTML = "You Worked " + minutesToHoursAndMinutes(totalMinutes)  + " hours";
  reset.style.opacity = 1;
}

function minutesToHoursAndMinutes(minutes) {
    const hours = Math.floor(minutes / 60);
    const mins = (minutes % 60);
    return (hours + '').padStart(2, '0') + ':' + (mins + '').padStart(2, '0')
}

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>

白馒头 2025-01-21 15:59:22

您可以使用 moment.js 来完成此操作,

请查看以下文章:

使用 moment js 计算两个日期的差异

或纯 JavaScript 代码

//sumTimes();

function sumTimes(){
 var day1 = Date.parse('01/01/1900 14:00');
 var day2 = Date.parse('01/01/1900 19:00');
 var diff = Math.abs(day2 - day1);
 console.log(diff);
}

这将产生毫秒级的差异。

You can do it by using moment.js

look the following article:

use moment js for difference two dates

or pure javascript code

//sumTimes();

function sumTimes(){
 var day1 = Date.parse('01/01/1900 14:00');
 var day2 = Date.parse('01/01/1900 19:00');
 var diff = Math.abs(day2 - day1);
 console.log(diff);
}

this will give difference by miliseconds.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文