JavaScript Tutorial/Date/Date Calculation — различия между версиями

Материал из Web эксперт
Перейти к: навигация, поиск
 
м (1 версия)
 
(нет различий)

Текущая версия на 08:25, 26 мая 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>