JavaScript Tutorial/Date/Date Calculation
Версия от 18:52, 25 мая 2010; (обсуждение)
Date substraction
<html>
<head>
<title>Date substraction</title>
</head>
<body>
<script language="JavaScript" type="text/javascript">
<!--
var myVar = prompt("Enter a future date: ")
var user_date = Date.parse(myVar);
var today_date = new Date();
var diff_date = user_date - today_date;
document.write(diff_date);
//-->
</script>
</body>
</html>
Get year, month and day from date difference
<html>
<head>
<title>Date substraction</title>
</head>
<body>
<script language="JavaScript" type="text/javascript">
<!--
var myVar = prompt("Enter a future date: ")
var user_date = Date.parse(myVar);
var today_date = new Date();
var diff_date = user_date - today_date;
var num_years = diff_date/31536000000;
var num_months = (diff_date % 31536000000)/2628000000;
var num_days = ((diff_date % 31536000000) % 2628000000)/86400000;
document.write("Number of years: " + Math.floor(num_years) + "<br>");
document.write("Number of months: " + Math.floor(num_months) + "<br>");
document.write("Number of days: " + Math.floor(num_days) + "<br>");
//-->
</script>
</body>
</html>