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();

  • 相关阅读:
    去除inline-block元素间间隙的几种方法
    数组去重的几种方法
    CSS实现水平居中的几种方法
    CSS实现垂直居中的几种方法
    实现一个jQuery的API
    jQuery从入门到放弃
    JavaScript中的DOM与BOM
    JavaScript中的原型与原型链
    爬取某东娃娃评价,生成词云
    vm提示:如果该虚拟机未在使用,请按“获取所有权(T)”按钮获取它的所有权。否则,请按“取消(C)”按钮以防损坏。
  • 原文地址:https://www.cnblogs.com/ms-grf/p/7931848.html
Copyright © 2011-2022 走看看