Native js Realizes Date Calculator Function
- 2021-07-21 07:24:31
- OfStack
In this paper, we share the specific implementation code of the date calculator for your reference, the specific contents are as follows
Date Calculator html Code Snippet:
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title> Date calculator </title>
<script type="text/javascript" src="date_calc.js"></script>
</head>
<body>
<br><br>
<b> Calculate the date a few days later: </b>
<br>
And
<input size="8" id="SY" value="2016"> Year
<input size="4" id="SM" value="11"> Month
<input size="4" id="SD" value="16"> Day
(Default is today)
<br> Difference
<input size="8" id="decday" value="100"> Days
(If you enter a negative number, it will be calculated forward)
<br> Yes:
<span id="result1"></span>
<br>
<input type=button value=" Calculation " onclick="cala()">
<br>
<b> Calculate date difference: </b>
<br>
<input size="8" id="SY2" value="2016"> Year
<input size="4" id="SM2" value="11"> Month
<input size="4" id="SD2" value="16"> Day
<br>
And <input size="8" id="SY3" value="2017" > Year
<input size="4" id="SM3" value="1"> Month
<input size="4" id="SD3" value="1"> Day
<br> Difference:
<span id="result2"></span> Days
<br>
<input type=button value=" Calculation " onclick="calb()">
<br>
</body>
</html>
date_calc. js code snippet:
var hzWeek= new Array(" Day ","1","2","3","4","5","6"," Day ");
// Acquisition week
function cweekday(wday)
{return hzWeek[wday];}
function cala()
{
y=document.getElementById("SY").value;
m=document.getElementById("SM").value;
d=document.getElementById("SD").value;
ddd=document.getElementById("decday").value;
ttt=new Date(y,m-1,d).getTime()+ddd*24000*3600;
theday=new Date();
theday.setTime(ttt);
document.getElementById("result1").innerHTML=theday.getFullYear()+" Year "+(1+theday.getMonth())+" Month "+theday.getDate()+" Day "+" Week "+cweekday(theday.getDay());
}
function calb()
{
y2=document.getElementById("SY2").value;
m2=document.getElementById("SM2").value;
d2=document.getElementById("SD2").value;
y3=document.getElementById("SY3").value;
m3=document.getElementById("SM3").value;
d3=document.getElementById("SD3").value;
day2=new Date(y2,m2-1,d2);
day3=new Date(y3,m3-1,d3);
document.getElementById("result2").innerHTML=(day3-day2)/86400000;
}