Home >Web Front-end >JS Tutorial >javascript calculate how many days you have lived

javascript calculate how many days you have lived

藏色散人
藏色散人Original
2021-07-01 10:55:573581browse

Javascript method to calculate how many days you have lived: first define your birthday; then pass "parseInt((today.getTime()-birthday.getTime())/(1000*3600*24));" The method is to calculate how many days you have lived.

javascript calculate how many days you have lived

The operating environment of this article: windows7 system, javascript version 1.8.5, DELL G3 computer

javascript method to calculate how many days you have been alive

Calculate how many days you have lived from birth to today and how old you are this year

The code is as follows:

<script type="text/javascript">
   
          var birthday=new Date("1996/06/21");//定义自己的生日
     var today=new Date();
  //getTime() 返回从 1970 年 1 月 1 日至今的毫秒数。
  //1秒=1000毫秒  3600=1小时*1分钟   24=24小时
    var time=parseInt((today.getTime()-birthday.getTime())/(1000*3600*24));
    var year=Math.round(time/365);//round() 方法可把一个数字舍入为最接近的整数。
      document.write("从出生到今天活了"+time+"天<br />");
       document.write("今年"+year+"岁");
</script >

Recommended study: "javascript Advanced Tutorial"

The above is the detailed content of javascript calculate how many days you have lived. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn