当初做统计业务需要处理时间 周报:本周 上周 下周 近一周    月报上月 本月  等 需要使用时间处理 所以扩展了这些方法 

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title></title>
</head>
<body> </body>
</html>
<script type="text/javascript">
var _DateTostring = Date.prototype.toString;
Date.prototype.add = function (type, value) {
if (!type || value==undefined||value==null || isNaN(value)) {
throw "增加的类型不存在或者值不是一个数字"
}
switch (type) {
case "d":
case "D":
this.setDate(this.getDate() + value);
break
case "M":
case "m":
this.setMonth(this.getMonth() + value);
break;
case "Y":
case "y":
this.setFullYear(this.getFullYear() + value);
break;
case "day":
case "DAY":
//本周的第几天
var day =this.getDay()==0?7:this.getDay(); var i = Math.abs(value);
if (value == 0) { this.addDate(-(day-1));//本周
return;
}
value < 0 ? this.addDate(-(day + 7 * i - 1)) : i > 1 ? this.addDate(7 - day + ((i - 1) * 7)+1) : this.addDate(7 - day+1); break;
}
var date = this;
return date;
}
Date.prototype.addDate = function (value) {
this.add("d", value);
}
Date.prototype.addMonth = function (value) {
this.add("m", value);
}
Date.prototype.addDay = function (value) {
this.add("day", value);
}
Date.prototype.addYear = function (value) {
this.add("y", value);
}
Date.prototype.toString = function (fmt) {
if (fmt == undefined) {
return _DateTostring.call(this);
}
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;
}
var date = new Date();
//date.addDate(1);//当前时间基础加上12天
//date.addDate(-1);//当前时间基础减去12天
//date.addMonth(-1);//当前时间基础减去1月
//date.addMonth(1);//当前时间基础加上一月
//date.addYear(1);//当前时间基础加上一年
//date.addYear(-1);//当前时间基础减去一年
//date.addDay(-1);//上周
//date.addDay(-2);//前2周
//date.addDay(0);//本周
//console.log(date.toString("yyyy-MM-dd"));//格式化日期
// console.log(date.toString());
// date.addDay(-1);//下周 </script>

最新文章

  1. Hive-0.x.x - Enviornment Setup
  2. 3分钟干货学会使用node-inspector调试NodeJS代码
  3. 标题编辑 AndroidTagGroup
  4. js让程序暂停一段时间再执行
  5. SwfUpload学习记录
  6. KVC - 键值编码
  7. qt画刷和画笔
  8. 港交所OMD-C对接笔记
  9. Linux指令--cd,pwd
  10. IOS设备设计完整指南
  11. [OC] 添加 pch 文件
  12. CSS3禁止用户选中文字——user-select: none;
  13. Hive快捷查询:不启用Mapreduce job启用Fetch task三种方式介绍
  14. Word中如何删除目录页的页码
  15. 【MyEclipse】JSP默认打开方式 设置(双击)
  16. Elastic 技术栈之快速入门
  17. 【纪中集训2019.3.27】【集训队互测2018】小A的旅行(白)
  18. 【转】IOCP创建
  19. PHP 神盾解密工具
  20. spring错误处理 Build path is incomplete. Cannot find class file for org.springframework.aop.Advisor

热门文章

  1. swift初始化
  2. android小技巧:在activity中实现与绑定的fragment的回调
  3. python中获取当前路径【os模块】
  4. ROS单线多拨pppoe
  5. C Linux read write function extension
  6. Meson使用
  7. OC数组和字典中存入niu值
  8. ios - masonry第三方库使用自动布局(参考:http://www.cocoachina.com/ios/20141219/10702.html)
  9. Educational Codeforces Round 33
  10. jquery mobile datepicker