首页 > 其他分享 >js AddDays ,AddYears

js AddDays ,AddYears

时间:2024-04-23 11:47:56浏览次数:18  
标签:function return fmt AddDays js var Date RegExp AddYears

//日期类型变量格式化,默认格式"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);
}

  

标签:function,return,fmt,AddDays,js,var,Date,RegExp,AddYears
From: https://www.cnblogs.com/itjeff/p/18152504

相关文章

  • three.js使用Instanced Draw+Frustum Cull+LOD来渲染大场景(开源)
    大家好,本文使用three.js实现了渲染大场景,在移动端也有较好的性能,并给出了代码,分析了关键点,感谢大家~关键词:three.js、InstancedDraw、大场景、LOD、FrustumCull、优化、Web3D、WebGL、开源代码:Github我正在承接Web3D数字孪生项目,具体介绍可看承接各种Web3D业务加QQ群交流:106......
  • fastjson导致的程序崩溃:A fatal error has been detected by the Java Runtime Enviro
    ##AfatalerrorhasbeendetectedbytheJavaRuntimeEnvironment:##EXCEPTION_ACCESS_VIOLATION(0xc0000005)atpc=0x000001da4d3ab6b3,pid=15996,tid=0x0000000000006478##JREversion:Java(TM)SERuntimeEnvironment(8.0_361)(build1.8.0_361-b09)......
  • gojs2.1去水印
    go.jsgo-debug.jsgo-module.jsES6模块go-debug-module.js这四个文件,具体看你使用哪一个搜索String.fromCharCode找到下图位置,这是我的格式化之后的样子 然后在 returnf 之前添加代码if(f.indexOf('GoJS2.1evaluation')>-1||f.indexOf('(c)1998-......
  • vis.js摄像机位置
    代码案例<!DOCTYPEhtml><html><head><title>Graph3Ddemo</title><style>body{font:10ptarial;}td{font:10ptarial;}</style><s......
  • vis.js绘制3d图形
    案例1代码案例<!DOCTYPEhtml><html><head><title>Graph3Ddemo</title><scripttype="text/javascript"src="https://unpkg.com/vis-graph3d@latest/dist/vis-graph3d.min.js"><......
  • vis.js分组多图折线图
    代码案例<!doctypehtml><html><head><title>Timeline</title><scripttype="text/javascript"src="https://unpkg.com/vis-timeline@latest/standalone/umd/vis-timeline-graph2d.min.js"></script>......
  • vis.js着色
    代码案例<!doctypehtml><html><head><title>Timeline</title><scripttype="text/javascript"src="https://unpkg.com/vis-timeline@latest/standalone/umd/vis-timeline-graph2d.min.js"></script>......
  • vis.js滚动折线图
    代码案例<!doctypehtml><html><head><title>Timeline</title><scripttype="text/javascript"src="https://unpkg.com/vis-timeline@latest/standalone/umd/vis-timeline-graph2d.min.js"></script>......
  • vis.js散点图
    代码案例<!doctypehtml><html><head><title>Timeline</title><scripttype="text/javascript"src="https://unpkg.com/vis-timeline@latest/standalone/umd/vis-timeline-graph2d.min.js"></script>......
  • vis.js滚动和排序
    代码案例<!doctypehtml><html><head><title>Timeline</title><scripttype="text/javascript"src="https://unpkg.com/vis-timeline@latest/standalone/umd/vis-timeline-graph2d.min.js"></script>......