js日期原型扩展
2024-08-31 08:13:58
当初做统计业务需要处理时间 周报:本周 上周 下周 近一周 月报上月 本月 等 需要使用时间处理 所以扩展了这些方法 <!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>
最新文章
- Hive-0.x.x - Enviornment Setup
- 3分钟干货学会使用node-inspector调试NodeJS代码
- 标题编辑 AndroidTagGroup
- js让程序暂停一段时间再执行
- SwfUpload学习记录
- KVC - 键值编码
- qt画刷和画笔
- 港交所OMD-C对接笔记
- Linux指令--cd,pwd
- IOS设备设计完整指南
- [OC] 添加 pch 文件
- CSS3禁止用户选中文字——user-select: none;
- Hive快捷查询:不启用Mapreduce job启用Fetch task三种方式介绍
- Word中如何删除目录页的页码
- 【MyEclipse】JSP默认打开方式 设置(双击)
- Elastic 技术栈之快速入门
- 【纪中集训2019.3.27】【集训队互测2018】小A的旅行(白)
- 【转】IOCP创建
- PHP 神盾解密工具
- spring错误处理 Build path is incomplete. Cannot find class file for org.springframework.aop.Advisor
热门文章
- swift初始化
- android小技巧:在activity中实现与绑定的fragment的回调
- python中获取当前路径【os模块】
- ROS单线多拨pppoe
- C Linux read write function extension
- Meson使用
- OC数组和字典中存入niu值
- ios - masonry第三方库使用自动布局(参考:http://www.cocoachina.com/ios/20141219/10702.html)
- Educational Codeforces Round 33
- jquery mobile datepicker