---- AI试用 ---域名问题某些图片和js资源无法访问,导致一些代码实例无法运行!(代码里gzui.net换成momen.vip即可)

JS计算宝宝出生后的年龄,两种表示方式,精确到秒哦

前端开发 蚂蚁 1536℃ 0评论

https://mangocool.com/1442561695415.html
http://sheng.momen.vip/
有了孩子之后,对小宝宝的事情会不刻意的增加很多关注,这可能就是做父亲的责任吧。

崽崽的周岁开始倒计时了,但是日子长了,算起来就比较费劲,也容易算错。本想依靠百度,没想到花了一晚竟找不到一款,能够计算宝宝年龄比较准确的软件!失望之余,我决定在工作之余写一个这样的计算小功能。于是就有了以下内容:

新鲜出炉!代码还有很多地方需要优化,甚至还有bug,但是仍然迫不及待的贴出来,跟网友们分享。

第一个计算方式,计算出宝宝从出生到现在的天数、时、分、秒:

//获取出生天数、时、分、秒
function getGrowTime(birthday)
{
    var currentTime = Date.parse(new Date());
    var myTime = new Date(birthday);
    var myTimeStamp = Date.parse(myTime);
    var gapSec = (currentTime - myTimeStamp)/1000;
    var day = 0;
    var hour = 0;
    var min = 0;
    var sec = 0;
    if(gapSec/(60*60*24) > 0)
    {
        day = (gapSec-gapSec%(60*60*24))/(60*60*24);
        gapSec = gapSec%(60*60*24);
        if((gapSec/3600) > 0)
        {
            hour = (gapSec-gapSec%3600)/3600;
            gapSec = gapSec%3600;
            if((gapSec/60) > 0)
            {
                min = (gapSec-gapSec%60)/60;
                sec = gapSec%60;
            } else
            {
                sec = gapSec;
            }
        } else
        {
            if((gapSec/60) > 0)
            {
                min = (gapSec-gapSec%60)/60;
                sec = gapSec%60;
            } else
            {
                sec = gapSec;
            }
        }
    } else
    {
        if((gapSec/3600) > 0)
        {
            hour = (gapSec-gapSec%3600)/3600;
            gapSec = gapSec%3600;
            if((gapSec/60) > 0)
            {
                min = (gapSec-gapSec%60)/60;
                sec = gapSec%60;
            } else
            {
                sec = gapSec;
            }
        } else
        {
            if((gapSec/60) > 0)
            {
                min = (gapSec-gapSec%60)/60;
                sec = gapSec%60;
            } else
            {
                sec = gapSec;
            }
        }
    }
    var dateStr = (day<10? ("0"+day): day)+"天 " + 
                  (hour<10? ("0"+hour): hour)+"时 " + 
                  (min<10? ("0"+min): min)+"分 " + 
                  (sec<10? ("0"+sec): sec)+"秒";
    return dateStr;
}

第二个计算方式,计算宝宝从出生到现在的周岁、月、天、时、分、秒,这个很常用:

//获取出生年龄,周岁、月、天、时、分、秒
function getGrowAge(birthday)
{
    var now = new Date();
    var year = now.getFullYear();
    var month = now.getMonth() + 1;
    var day = now.getDate();
    var hour = now.getHours();
    var minute = now.getMinutes();
    var second = now.getSeconds();

    var myDate = new Date(birthday);
    var myYear = myDate.getFullYear();
    var myMonth = myDate.getMonth() + 1;
    var myDay = myDate.getDate();
    var myHour = myDate.getHours();
    var myMinute = myDate.getMinutes();
    var mySecond = myDate.getSeconds();

    var gapSecond = second - mySecond;
    if(gapSecond < 0)
    {
        minute -= 1;
        gapSecond = 60 - mySecond + second;
    }
    var gapMinute = minute - myMinute;
    if(gapMinute < 0)
    {
        hour -= 1;
        gapMinute = 60 - myMinute + minute;
    }
    var gapHour = hour - myHour;
    if(gapHour < 0)
    {
        day -= 1;
        gapHour = 24 - myHour + hour;
    }
    var gapDay = day - myDay;
    if(gapDay < 0)
    {
        month -= 1;
        gapDay = getDaysOfMonth(birthday) - myDay + day;
    }
    var gapMonth = month - myMonth;
    if(gapMonth < 0)
    {
        year -= 1;
        gapMonth = 12 - myMonth + month;
    }
    var gapYear = year - myYear;
    if(gapYear < 0)
    {
        gapYear = 0;
    }

    var dateStr = gapYear+"岁 " + 
                  (gapMonth<10? ("0"+gapMonth): gapMonth)+"月 " + 
                  (gapDay<10? ("0"+gapDay): gapDay)+"天 " + 
                  (gapHour<10? ("0"+gapHour): gapHour)+"时 " + 
                  (gapMinute<10? ("0"+gapMinute): gapMinute)+"分 " + 
                  (gapSecond<10? ("0"+gapSecond): gapSecond)+"秒";
    return dateStr;
}

还有两个计算所须的方法:

//获取当月的天数
function getDaysOfMonth(dateStr)
{   
    var date = new Date(dateStr);
    var year = date.getFullYear();
    var mouth = date.getMonth() + 1;
    var day = 0;

    if(mouth == 2)
    {
        day= isLeapYear(year) ? 29 : 28;
    } else if(mouth == 1 || mouth == 3 || mouth == 5 || 
              mouth == 7 || mouth == 8 || mouth == 10 || mouth == 12)
    {
        day= 31;
    } else
    {
        day= 30;
    }
    return day;
}

//判断是否为闰年
function isLeapYear(year)
{
    return (year%4==0 && year%100!=0)||(year%400==0);
}

提示:你可以先修改部分代码再运行。

转载请注明:有爱前端 » JS计算宝宝出生后的年龄,两种表示方式,精确到秒哦

喜欢 (0)or分享 (0)