zoukankan      html  css  js  c++  java
  • 时间处理+年龄计算

    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岁

    获取当天凌晨的时间戳

    var today = new Date(new Date().setHours(0, 0, 0, 0)).getTime();

  • 相关阅读:
    8.25
    8.24
    8.23
    8.22
    8.21
    8.20
    8.19
    8.18
    8.17
    8.16
  • 原文地址:https://www.cnblogs.com/ms-grf/p/7931848.html
Copyright © 2011-2022 走看看