javascript算出活了多少天的方法:首先定義自己的生日;然后通過“parseInt((today.getTime()-birthday.getTime())/(1000*3600*24));”方法計算活了多少天即可。
本文操作環(huán)境:windows7系統(tǒng)、javascript1.8.5版,DELL G3電腦
javascript算出活了多少天的方法
計算你從出生到今天活了多少天,今年多少歲
代碼如下:
<script type="text/javascript"> var birthday=new Date("1996/06/21");//定義自己的生日 var today=new Date(); //getTime() 返回從 1970 年 1 月 1 日至今的毫秒數(shù)。 //1秒=1000毫秒 3600=1小時*1分鐘 24=24小時 var time=parseInt((today.getTime()-birthday.getTime())/(1000*3600*24)); var year=Math.round(time/365);//round() 方法可把一個數(shù)字舍入為最接近的整數(shù)。 document.write("從出生到今天活了"+time+"天<br />"); document.write("今年"+year+"歲"); </script >
推薦學習:《javascript高級教程》