我怎样才能制作一个程序来读取日期(小时,分钟,秒)然后从第二个日期中减去它
How can I make a program that reads the date(hour,minutes,seconds) then it subtracts it from the second date
我有一个任务是创建一个程序,用户在其中给出小时、分钟、秒,然后他给出另一个小时、分钟、秒,然后程序应该从第一个日期中减去第二个日期。
我一直在尝试不使用日期格式,但无法正常工作。
日期必须是 24 小时格式,并且这两个日期必须是同一天。所以如果我写 12:59:59
那么我不能将第二个日期设置为 12:59:58
只是 13:00:00
.
DateSubtraction = function( date1, date2 ) {
// Convert both dates to milliseconds
var date1_ms = date1.getTime();
var date2_ms = date2.getTime();
// Calculate the difference in milliseconds
var difference_ms = date2_ms - date1_ms;
console.log(difference_ms);
}
//Set the two dates
var y2k = new Date(2000, 0, 1);
var Jan1st2010 = new Date(y2k.getFullYear() + 10, y2k.getMonth(), y2k.getDate());
var today= new Date();
DateSubtraction(Jan1st2010, today);
您将必须针对用户输入进行修改
这是我将如何做的方法。
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>
我有一个任务是创建一个程序,用户在其中给出小时、分钟、秒,然后他给出另一个小时、分钟、秒,然后程序应该从第一个日期中减去第二个日期。
我一直在尝试不使用日期格式,但无法正常工作。
日期必须是 24 小时格式,并且这两个日期必须是同一天。所以如果我写 12:59:59
那么我不能将第二个日期设置为 12:59:58
只是 13:00:00
.
DateSubtraction = function( date1, date2 ) {
// Convert both dates to milliseconds
var date1_ms = date1.getTime();
var date2_ms = date2.getTime();
// Calculate the difference in milliseconds
var difference_ms = date2_ms - date1_ms;
console.log(difference_ms);
}
//Set the two dates
var y2k = new Date(2000, 0, 1);
var Jan1st2010 = new Date(y2k.getFullYear() + 10, y2k.getMonth(), y2k.getDate());
var today= new Date();
DateSubtraction(Jan1st2010, today);
您将必须针对用户输入进行修改
这是我将如何做的方法。
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>