码迷,mamicode.com
首页 > 其他好文 > 详细

时间处理+年龄计算

时间:2017-11-30 21:42:46      阅读:180      评论:0      收藏:0      [点我收藏+]

标签:fun   eth   nbsp   fine   etag   parse   turn   col   date()   

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岁

时间处理+年龄计算

标签:fun   eth   nbsp   fine   etag   parse   turn   col   date()   

原文地址:http://www.cnblogs.com/ms-grf/p/7931848.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!