在javascript中使用毫秒计算月和日

时间:2017-06-04 09:19:55

标签: javascript date

我想用毫秒来计算剩余的月份和天数。

以下代码..i计算用户输入出生日期的年数,但我如何计算剩余月份和天数。

<!DOCTYPE html>
<html>
<head>
</head>
<body>

<h1>A Web Page</h1>
<input type="text "id="demo">
<button type="button" onclick="myFunction()">Try it</button>

</body>
</html>
<script>
function myFunction() {
	var a=document.getElementById("demo").value;
	var birthdate = new Date(a);
	var cur = new Date();
	var diff = cur-birthdate; // This is the difference in milliseconds
	var age = Math.floor(diff/31557600000); // Divide by 1000*60*60*24*365.25
     var month=;//i want to calcuate this using millsecond only
     var days=;//i want to calcuate this using millsecond only


	alert("Hi ..you are "+ age+"and"+month+"and"+days+" year old Now");
}
</script>

帮助mi谢谢..

1 个答案:

答案 0 :(得分:0)

从分组的其余部分计算月数和不足

var remainder = diff % 31557600000;
var month = Math.floor(remainder / (1000*60*60*24*30.5));
var remainder2 = remainder % (1000*60*60*24*30.5);
var days = Math.floor(remainder2 / (1000*60*60*24));