js AddDays ,AddYears

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
//日期类型变量格式化,默认格式"xxxx-xx-xx"
Date.prototype.Format = function (fmt) {
    fmt = fmt || "yyyy-MM-dd";
    var o = {
        "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+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
    for (var k in o)
        if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
    return fmt;
}
 
//添加天
Date.prototype.AddDays = function (d) {
    this.setDate(this.getDate() + d);
    return this;
}
 
//添加周
Date.prototype.AddWeeks = function (w) {
    this.addDays(w * 7);
    return this;
}
 
//添加月
Date.prototype.AddMonths = function (m) {
    var d = this.getDate();
    this.setMonth(this.getMonth() + m);
 
    if (this.getDate() < d)
        this.setDate(0);
 
    return this;
}
 
//添加年
Date.prototype.AddYears = function (y) {
    var m = this.getMonth();
    this.setFullYear(this.getFullYear() + y);
 
    if (m < this.getMonth()) {
        this.setDate(0);
    }
 
    return this;
}
 
//日期加减函数,strDate传入你需要的日期,格式"xxxx-xx-xx"。days传要加减的日期数,往前传正数,往后传负数
function AddDate(strDate, days, fmt) {
    var date = new Date(strDate);
    date.setDate(date.getDate() + days);
    return date.Format(fmt);
}

  

posted on   itjeff  阅读(87)  评论(0编辑  收藏  举报

相关博文:
阅读排行:
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· .NET10 - 预览版1新功能体验(一)
历史上的今天:
2019-04-23 在.txt文件的首行写上.LOG后,后面每次对改文本文件进行编辑后,系统会自动在编辑内容后记录操作时间
2018-04-23 HTTPS简单原理介绍

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5
点击右上角即可分享
微信分享提示