• node.js日期


    废话不多说了,就是一些日期格式以及一些小方法,直接写在Date.prototype里了,new Date()直接就可以用了。

    Date.prototype.format = function (format) {
        var o = {
            "M+":this.getMonth() + 1, //month
            "d+":this.getDate(), //day
            "h+":this.getHours(), //hour
            "m+":this.getMinutes(), //minute
            "s+":this.getSeconds(), //second
            "q+":Math.floor((this.getMonth() + 3) / 3), //quarter
            "S":this.getMilliseconds() //millisecond
        }
    
        if (/(y+)/.test(format)) {
            format = format.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
        }
    
        for (var k in o) {
            if (new RegExp("(" + k + ")").test(format)) {
                format = format.replace(RegExp.$1, RegExp.$1.length == 1 ? o[k] : ("00" + o[k]).substr(("" + o[k]).length));
            }
        }
        return format;
    }
    Date.prototype.timestamp = function () {
        return this.getTime();
    }
    Date.prototype.DateAdd = function (strInterval, Number) {
        var dtTmp = this;
        switch (strInterval) {
            case 's' :
                return new Date(Date.parse(dtTmp) + (1000 * Number));
            case 'n' :
                return new Date(Date.parse(dtTmp) + (60000 * Number));
            case 'h' :
                return new Date(Date.parse(dtTmp) + (3600000 * Number));
            case 'd' :
                return new Date(Date.parse(dtTmp) + (86400000 * Number));
            case 'w' :
                return new Date(Date.parse(dtTmp) + ((86400000 * 7) * Number));
            case 'q' :
                return new Date(dtTmp.getFullYear(), (dtTmp.getMonth()) + Number * 3, dtTmp.getDate(), dtTmp.getHours(), dtTmp.getMinutes(), dtTmp.getSeconds());
            case 'm' :
                return new Date(dtTmp.getFullYear(), (dtTmp.getMonth()) + Number, dtTmp.getDate(), dtTmp.getHours(), dtTmp.getMinutes(), dtTmp.getSeconds());
            case 'y' :
                return new Date((dtTmp.getFullYear() + Number), dtTmp.getMonth(), dtTmp.getDate(), dtTmp.getHours(), dtTmp.getMinutes(), dtTmp.getSeconds());
        }
    }
    Date.prototype.DateDiff = function (strInterval, dtEnd) {
        var dtStart = this;
        if (typeof dtEnd == 'string')//如果是字符串转换为日期型
        {
            dtEnd = StringToDate(dtEnd);
        }
        switch (strInterval) {
            case 's' :
                return parseInt((dtEnd - dtStart) / 1000);
            case 'n' :
                return parseInt((dtEnd - dtStart) / 60000);
            case 'h' :
                return parseInt((dtEnd - dtStart) / 3600000);
            case 'd' :
                return parseInt((dtEnd - dtStart) / 86400000);
            case 'w' :
                return parseInt((dtEnd - dtStart) / (86400000 * 7));
            case 'm' :
                return (dtEnd.getMonth() + 1) + ((dtEnd.getFullYear() - dtStart.getFullYear()) * 12) - (dtStart.getMonth() + 1);
            case 'y' :
                return dtEnd.getFullYear() - dtStart.getFullYear();
        }
    }
    Date.prototype.DatePart = function (interval) {
        var myDate = this;
        var partStr = '';
        var Week = ['日', '一', '二', '三', '四', '五', '六'];
        switch (interval) {
            case 'y' :
                partStr = myDate.getFullYear();
                break;
            case 'm' :
                partStr = myDate.getMonth() + 1;
                break;
            case 'd' :
                partStr = myDate.getDate();
                break;
            case 'w' :
                partStr = Week[myDate.getDay()];
                break;
            case 'ww' :
                partStr = myDate.WeekNumOfYear();
                break;
            case 'h' :
                partStr = myDate.getHours();
                break;
            case 'n' :
                partStr = myDate.getMinutes();
                break;
            case 's' :
                partStr = myDate.getSeconds();
                break;
        }
        return partStr;
    }
    Date.prototype.WeekNumOfYear = function () {
        var myDate = this;
        var ary = myDate.toArray();
        var year = ary[0];
        var month = ary[1] + 1;
        var day = ary[2];
        document.write('< script language=VBScript> 
    ');
        document.write('myDate = DateValue("' + month + '-' + day + '-' + year + '") 
    ');
        document.write("result = DatePart('ww', myDate) 
    ");
        document.write(' 
    ');
        return result;
    }
    Date.prototype.isLeapYear = function () {
        return (0 == this.getYear() % 4 && ((this.getYear() % 100 != 0) || (this.getYear() % 400 == 0)));
    }
    Date.prototype.MaxDayOfDate = function () {
        var myDate = this;
        var month = myDate.getMonth() + 1;
        switch (month) {
            case 1:
            case 3:
            case 5:
            case 7:
            case 8 :
            case 10:
            case 12:
                return 31;
    
            case 4:
            case 6 :
            case 9 :
            case 11:
                return 30;
    
            case 2:
                return   myDate.isLeapYear() ? 28 : 29;
        }
    
    }
    
    function StringToDate(DateStr) {
    
        var converted = Date.parse(DateStr);
        var myDate = new Date(converted);
        if (isNaN(myDate)) {
            //var delimCahar = DateStr.indexOf('/')!=-1?'/':'-';
            var arys = DateStr.split('-');
            myDate = new Date(arys[0], --arys[1], arys[2]);
        }
        return myDate;
    }
    

      

  • 相关阅读:
    选择结构
    Java基本语法
    Java开发实战经典
    限时福利丨全网最全Java视频教程合集(java学习路线+视频+配套资料)
    入选《2021爱分析·区域性银行数字化厂商全景报告》,网易云信助力南京银行打造转型标杆
    资讯|WebRTC M94 更新
    网易云信的音频共享技术 | 体验共享专题
    网易云信 NERTC 高清画质体验之 H.265的工程实践 | 体验共享技术专题
    Jvm-Sandbox-Repeater 推送配置详解
    Jvm-Sandbox-Repeater 拉取配置详解
  • 原文地址:https://www.cnblogs.com/racyily/p/3651814.html
Copyright © 2020-2023  润新知