通过输入两个日期来计算天数
计算当前日期的天数
找到从当前日期到圣诞节的剩余天数
在JavaScript中定义两个日期变量
通过使用 new Date() 创建日期对象来初始化它们.
定义日期之后,使用 date2.getTime()-date1.getTime(); ,从另一个日期减去一个日期,即可计算日期之间的时差。
现在下一步是计算日期之间的天数。因此,我们将两个日期的时差除以一天中的毫秒数,即1000 * 60 * 60 * 24
最后,打印使用 document.write()方法计算的结果。
<html>
<head>
<title>Calculate days by entering two dates</title>
</head>
<body>
<script>
var date1, date2;
//define two date object variables with dates inside it
date1 = new Date("07/15/2015");
date2 = new Date("11/28/2016");
//calculate time difference
var time_difference = date2.getTime() - date1.getTime();
//calculate days difference by dividing total milliseconds in a day
var days_difference = time_difference / (1000 * 60 * 60 * 24);
document.write("Number of days between dates <br>" +
date1 + " and <br>" + date2 + " are: <br>"
+ days_difference + " days");
</script>
</body>
</html>
<html>
<head>
<title> Calculate days between dates </title>
<script>
function daysDifference() {
//define two variables and fetch the input from HTML form
var dateI1 = document.getElementById("dateInput1").value;
var dateI2 = document.getElementById("dateInput2").value;
//define two date object variables to store the date values
var date1 = new Date(dateI1);
var date2 = new Date(dateI2);
//calculate time difference
var time_difference = date2.getTime() - date1.getTime();
//calculate days difference by dividing total milliseconds in a day
var result = time_difference / (1000 * 60 * 60 * 24);
return document.getElementById("result").innerHTML =
result + " days between both dates. ";
}
</script>
</head>
<body>
<h2 style="color: 32A80F" align="Center">
lidihuo : Calculate days between dates
<br> <br> </h2>
<p align="Center"> <b> Enter date1 </b>
<input type="date" id="dateInput1">
<br> <br>
<p align="Center"> <b> Enter date2 </b>
<input type="date" id="dateInput2">
<br> <br>
<button onclick="daysDifference()">
Calculate number of days
</button>
</p>
<p style="color:32A80F" id="result" align="center"></p>
</body>
</html>
Enter date1
Enter date2
定义两个日期变量,即date1和date2
通过使用 new Date()创建日期对象来初始化date1变量,默认情况下将采用系统日期。
使用 new Date()创建日期对象来初始化date2变量,并在其中提供日期。
现在使用Math.abs()方法来计算两个日期之间的总秒数。因此,将毫秒之间的日期差除以毫秒数 Math.abs(date2-date1)/1000;
现在下一步是使用 Math.floor()方法计算天数。因此,将上一步的计算结果(两个日期之间的总秒数)除以60 * 60 * 24
最后,打印使用 document.write()方法计算的结果。
<html>
<head>
<title>Calculate days between the dates using default system dates</title>
</head>
<body>
<script>
var date1, date2;
//define two date object variables with dates inside it
date1 = new Date();
date2 = new Date ("Dec 15, 2021, 21:45:10");
//calculate total number of seconds between two dates
var total_seconds = Math.abs(date2 - date1) / 1000;
//calculate days difference by dividing total seconds in a day
var days_difference = Math.floor (total_seconds / (60 * 60 * 24));
document.write("Number of days between dates from current date <br>" +
date1 + " and <br>" + date2 + " are: <br>"
+ days_difference + " days");
</script>
</body>
</html>
通过使用 new Date()创建日期对象来定义当前日期(系统日期)。
使用新的Date()类方法再定义一个日期,并通过其中的 date.getFullYear()方法获取圣诞节日期。 date.getFullYear()方法将返回年份(JavaScript中为0-11个月)。
如果今年圣诞节已经过世,它将返回从当前日期到明年圣诞节剩余的天数。
现在,该时间使用Math.round()函数计算结果了(以毫秒为单位),然后将其转换为天数。因此,将 Math.round(Christmas()-present_date.getTime())除以一天中的总毫秒数,即1000 * 60 * 60 * 24。
从结果中删除十进制值,并使用 document.write()方法将其显示在Web上。
<html>
<head>
<title>Calculate days remaining in Christmas</title>
</head>
<body>
<script>
//declare two variables in JavaScript
var current_date, christmas_day;
//total milliseconds in one day
var one_day_ms = 1000 * 60 * 60 * 24;
//set the current date in the variable
current_date = new Date();
//set the Christmas date in another variable where 0-11 is month in JavaScript
christmas_day= new Date(current_date.getFullYear(), 11, 25);
//calculate next year Christmas if this year?s date is already passed away
if (current_date.getMonth() == 11 && current_date.getdate() > 25)
{
christmas_day.setFullYear(christmas_day.getFullYear() + 1);
}
//calculate result in milliseconds and convert it into days
var res = Math.round(christmas_day.getTime() - current_date.getTime()) / (one_day_ms);
//remove the decimal point from the calculated result
var FinalResult = res.toFixed (0);
//display the final result on web
document.write("Number of days remaining in Christmas day: <br>" +
current_date + " and <br>" + christmas_day + " are: <br>" + FinalResult + " days");
</script>
</body>
</html>