没有合适的资源?快使用搜索试试~ 我知道了~
Javascript 日期对象Date扩展方法
0 下载量 83 浏览量
2020-10-29
22:40:29
上传
评论
收藏 57KB PDF 举报
温馨提示
试读
7页
Date对象扩展,包括常用中文日期格式解析、加减操作、日期差、周操作和季操作。
资源推荐
资源详情
资源评论
Javascript 日期对象日期对象Date扩展方法扩展方法
Date对象扩展,包括常用中文日期格式解析、加减操作、日期差、周操作和季操作。
今天在网上摘抄了些js中操作日期的相关方法,现在与大家分享一下。
复制代码 代码如下:
<script type="text/javascript">
Date.prototype.Format = function(fmt)
{
//author: meizz
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);
};
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);
}
};
//测试 var now = new Date(); now.addDays(1);//加减日期操作 alert(now.Format("yyyy-MM-dd"));
Date.prototype.dateDiff = function(interval,endTime)
{
switch (interval)
{
case "s": //計算秒差
return parseInt((endTime-this)/1000);
case "n": //計算分差
return parseInt((endTime-this)/60000);
case "h": //計算時差
return parseInt((endTime-this)/3600000);
case "d": //計算日差
return parseInt((endTime-this)/86400000);
case "w": //計算週差
return parseInt((endTime-this)/(86400000*7));
资源评论
weixin_38642897
- 粉丝: 2
- 资源: 896
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功