zoukankan      html  css  js  c++  java
  • 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;
    }
    

      

  • 相关阅读:
    【系统安全性】一、背景、原理与解决思路
    Spring Boot使用阿里云证书启用HTTPS
    图片上传前先在本地预览
    gulp的使用以及Gulp新手入门教程
    bootstrap datetimepicker、bootstrap datepicker日期组件对范围的简单封装
    作为程序员,你关注哪些国外 IT 网站?
    sql server 2014安装后用sa登录问题
    win server2012r2上发布网站常见错误 "HTTP 错误 500.19 请求的页面的相关配置数据无效" 解决办法
    dapper使用时性能优化
    解决Web部署 svg/woff/woff2字体 404错误
  • 原文地址:https://www.cnblogs.com/racyily/p/3651814.html
Copyright © 2011-2022 走看看