首页 > 代码库 > javascript时间函数

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;}

 

javascript时间函数