我正在尝试编写一个程序,使用JavaScript解决两个日期之间的差异.我想要比较的两个日期是使用日期格式的HTML表单输入的.我想使用javaScript比较两个输入的日期,具体来说,找出整周的两个日期之间的差异,然后是剩余的日期.我已经设法比较javaScript中的两个硬编码日期,但是在表单中有两个日期时遇到了这个问题.任何指针将非常感谢!以下是javaScript程序:
<!DOCTYPE html>
<html>
<title> Dates </title>
<head>Class Test
<script language="javascript">
function Ict5() { // ALERT BOX
today = new Date(); //Using new Date(), creates a new date object with the current date and time
birthday = new Date("March 28, 1995"); //using new date with stuff in the brackets, assigns the date to the variable
birthday.setFullYear(1995);
msPerDay = 24 * 60 * 60 * 1000;
msPerWeek = 7 * 24 * 60 * 60 * 1000;
msBetween = (today.getTime() - birthday.getTime()); //get time Returns the number of milliseconds since midnight Jan 1, 1970
daysBetween = Math.round(msBetween/msPerDay); //math round returns the value of a number rounded to the nearest integer.
weeksBetween = Math.floor(msBetween/msPerWeek);
window.alert("There are " +daysBetween+ " days or " +weeksBetween+ " full weeks between my date of birth and today ");
}
</script>
</head>
<body>
<H1> An Example of Form </H1>
<Form action="process.pl" method="POST" name="MyForm" onsubmit="return formValidation()">
<p>Enter Date:</p>
<input type=date name="date1" id="date1">
<input type=date name="date2" id="date2">
<button type="button" onclick="Ict5()">Calculate Fare</button><br>
</Form>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
您可以从输入中获取值,如下所示:
var date1 = new Date(document.getElementById("date1").value);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1441 次 |
| 最近记录: |