zoukankan      html  css  js  c++  java
  • javascript时间函数

    //js格式化时间
    //"yyyy-MM-dd hh:mm:ss"
    Date.prototype.format = function (format) {
        var date = {
            "M+": this.getMonth() + 1,
            "d+": this.getDate(),
            "h+": this.getHours(),
            "m+": this.getMinutes(),
            "s+": this.getSeconds(),
            "q+": Math.floor((this.getMonth() + 3) / 3),
            "S+": this.getMilliseconds()
        };
        if (/(y+)/i.test(format)) {
            format = format.replace(RegExp.$1, (this.getFullYear() + '').substr(4 - RegExp.$1.length));
        }
        for (var k in date) {
            if (new RegExp("(" + k + ")").test(format)) {
                format = format.replace(RegExp.$1, RegExp.$1.length == 1
                                ? date[k] : ("00" + date[k]).substr(("" + date[k]).length));
            }
        }
        return format;
    }
    //时间加天数
    Date.prototype.addDays = function (day, format) {
        this.setDate(this.getDate() + day);
        return this.format(format);
    }
    //时间加周数
    Date.prototype.addWeeks = function (w) {
        this.addDays(w * 7);
    };
    //时间加月数
    Date.prototype.addMonths = function (m) {
        var d = this.getDate();
        this.setMonth(this.getMonth() + m);
    
        if (this.getDate() < d)
            this.setDate(0);
    };
    //时间加年数
    Date.prototype.addYears = function (y) {
        var m = this.getMonth();
        this.setFullYear(this.getFullYear() + y);
    
        if (m < this.getMonth()) {
            this.setDate(0);
        }
    };
    //根据日期,起得第几周
    Date.prototype.getWeek = function () {
        return new Array("星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六")[this.getDay()];
    }
    //获取参数,如:字符串('?a=hello&b=world'),window.location.search.parameter('a');
    String.prototype.parameter = function (name) {
        var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
        var r = this.substr(1).match(reg);
        if (r != null) return unescape(r[2]); return null;
    }
    
  • 相关阅读:
    20205025模拟
    CDQ分治详解
    点分治详解
    虚树详解
    整体二分详解
    算法学习————线段树合并
    Mvc.ExceptionHandling.AbpExceptionFilter
    小程序前端转换时间格式2021-02-25T12:01:20.254748
    《生命3.0—在亿年的尺度下审视生命的演进》阅读笔记1
    软件杯赛题周总结(1)
  • 原文地址:https://www.cnblogs.com/sntetwt/p/4208059.html
Copyright © 2011-2022 走看看