我该如何制作一个程序来读取日期(小时,分钟,秒),然后从第二个日期减去日期 [英] How can I make a program that reads the date(hour,minutes,seconds) then it subtracts it from the second date
本文介绍了我该如何制作一个程序来读取日期(小时,分钟,秒),然后从第二个日期减去日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个任务是创建一个程序,其中用户给出了时,分,秒,然后又给出了时,分,秒,那么程序应该从第一个减去第二个日期.
I have a task to create a program where the user gives the Hour, Minute, Second, then he gives another Hour, Minute, Second then the program should substract the second date from the first one.
我一直在尝试不使用日期格式,但是我无法使其正常工作.
I have been trying to not use the date format, but I can't get it to work.
日期必须为24小时格式,并且这两个日期必须在同一天.因此,如果我写 12:59:59
,那么我就不能将第二个日期设置为 12:59:58
,而只是将 13:00:00
.
The date has to be in a 24h format, and these two dates have to be in the same day. So if I write 12:59:59
then I couldn't set the second date to 12:59:58
just 13:00:00
.
推荐答案
这是一种我将如何做的方法.
Here is an approach how i would do it.
var hours1, mins1, secs1, hours2, mins2, secs2;
/**
* get html element by id.
* @param {string} id give the html element id
* @return {object} document.getElementById(id);
*/
function GetId(id) { return document.getElementById(id) }
function subDate() {
hours1 = GetId("hours1").value;
mins1 = GetId("mins1").value;
secs1 = GetId("secs1").value;
hours2 = GetId("hours2").value;
mins2 = GetId("mins2").value;
secs2 = GetId("secs2").value;
// Set times to seconts
let time1 = ((mins1 * 60) + (hours1 * 3600)) + Number(secs1),
time2 = ((mins2 * 60) + (hours2 * 3600)) + Number(secs2);
let hours = 0,
mins = 0,
// Subtract all seconds.
secs = time2 - time1;
// Count all hours
while (secs >= 3600) {
hours +=1 ;
secs -= 3600;
}
// Count all mins
while (secs >= 60) {
mins += 1;
secs -= 60;
}
// Show anwser
let html = GetId("ans");
html.innerHTML = hours + ":" + mins + ":" + secs;
}
<p>
<input type="number" id="hours1" value="0" min="0" max="24">:
<input type="number" id="mins1" value="0" min="0" max="59">:
<input type="number" id="secs1" value="0" min="0" max="59">
</p>
<p>
<input type="number" id="hours2" value="0" min="0" max="24">:
<input type="number" id="mins2" value="0" min="0" max="59">:
<input type="number" id="secs2" value="0" min="0" max="59">
</p>
<button onclick="subDate()">Sup dates</button>
<p id="ans"></p>
这篇关于我该如何制作一个程序来读取日期(小时,分钟,秒),然后从第二个日期减去日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文