时间处理+年龄计算

Posted 发福大叔

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了时间处理+年龄计算相关的知识,希望对你有一定的参考价值。

var HrDate = function () {
    return {
        Y_MM_DD: "y-mm-dd",
        YY_MM_DD: "yy-mm-dd",
        HH_MM_SS: "HH:mm:ss",
        YY_MM_DD_HH_MM_SS: "yy-mm-dd HH:mm:ss",

        formatDate: function (date, format) {
            if (null !== date && undefined !== date) {
                return $.datepicker.formatDate(format, date);
            }
            return null;
        },
        formatTime: function (time, format) {
            if (null !== time && undefined !== time) {
                return $.datepicker.formatTime(format, {
                    hour: time.getHours(),
                    minute: time.getMinutes(),
                    second: time.getSeconds(),
                    millisec: time.getMilliseconds(),
                    timezone: time.getTimezoneOffset()
                });
            }
            return null;
        },
        formatDateTime: function (dateTime, dateFormat, timeFormat) {
            if (null !== dateTime && undefined !== dateTime) {
                return this.formatDate(dateTime, dateFormat) + " " + this.formatTime(dateTime, timeFormat);
            }
            return null;
        },
        getAgeFromBirthday: function (birth) {
            if (birth) {
                var newDate = new Date();
                var currentYear = newDate.getFullYear();
                var currentMonth = newDate.getMonth() + 1;
                var currentDay = newDate.getDate();

                var birthYear = birth.split("-")[0];
                var birthMonth = birth.split("-")[1];
                var birthDay = birth.split("-")[2];

                var myYear = currentYear - parseInt(birthYear);
                var myMonth = currentMonth - parseInt(birthMonth);
                var myDay = currentDay - parseInt(birthDay);

                var s = "";
                if (myDay < 0) {
                    myDay = myDay + 30;
                    myMonth--;
                }
                if (myMonth < 0) {
                    myMonth = myMonth + 12;
                    myYear--;
                }
                if ((myYear <= 0)) {
                    if (myMonth == 0) {
                        s = s + myDay + "天";
                    }
                    else {
                        s = s + myMonth + "月" + myDay + "天";
                    }
                }
                else {
                    s = myYear + "岁";
                    if (myYear < 6) {
                        s = s + myMonth + "月";
                    }
                }
                if (s == "0天") {
                    s = "1天";
                }
                return s;
            }
        }
    };
}();

----------------------formatDate----------------

HrDate.formatDate(new Date(),HrDate.YY_MM_DD) = 2017-11-30

----------------------formatTime----------------

HrDate.formatDate(new Date(),"HH时mm分") = 20时58分

HrDate.formatDate(new Date(),"HH:mm") = 20:58

----------------------formatDateTime----------------

HrDate.formatDateTime(new Date(),HrDate.YY_MM_DD,"HH时mm分") = 2017-11-30 20时58分

----------------------getAgeFromBirthday----------------

HrDate.getAgeFromBirthday("1992-05-13") = 25岁

以上是关于时间处理+年龄计算的主要内容,如果未能解决你的问题,请参考以下文章

如何让我的代码从出生日期计算年龄?

oracle 关于年龄计算问题?

SQL查询中年龄计算的性能

从 BirthDate 计算年龄

使用从循环内的代码片段中提取的函数避免代码冗余/计算开销

基于HTML/CSS/JS的年龄计算器 | 带有免费源码