JS获取本周周一,周末及获取任意时间的周一周末功能示例
本文实例讲述了JS获取本周周一,周末及获取任意时间的周一周末功能。分享给大家供大家参考,具体如下:
项目需要获取本周及任意一天的周一及周末需格式化,示例代码如下:
<!DOCTYPEhtmlPUBLIC"-//W3C//DTDXHTML1.0Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<htmlxmlns="http://www.w3.org/1999/xhtml">
<head>
<metahttp-equiv="Content-Type"content="text/html;charset=utf-8"/>
<title>星期</title>
<scripttype="text/javascript"language="javascript">
/**
*@authorzhuyangxing
*/
(function(){
functionutil_date(){
var_today=newDate();
this.today=_today;
this.year=_today.getYear()+1900;//当前年份
this.Month_a=_today.getMonth();
this.Month=this.Month_a+1;//当前月份
this.day=_today.getDate();//当前日期
this.date=_today.getDay()==0?7:_today.getDay();//本周第几天因系统会把周日作为第0天
this.Monday="";
this.Sunday="";
this.day_one="";
}
Date.prototype.pattern=function(fmt){
varo={
"M+":this.getMonth()+1,//月份
"d+":this.getDate(),//日
"h+":this.getHours()%12==0?12:this.getHours()%12,//小时
"H+":this.getHours(),//小时
"m+":this.getMinutes(),//分
"s+":this.getSeconds(),//秒
"q+":Math.floor((this.getMonth()+3)/3),//季度
"S":this.getMilliseconds()//毫秒
};
varweek={
"0":"/u65e5",
"1":"/u4e00",
"2":"/u4e8c",
"3":"/u4e09",
"4":"/u56db",
"5":"/u4e94",
"6":"/u516d"
};
if(/(y+)/.test(fmt)){
fmt=fmt.replace(RegExp.$1,(this.getFullYear()+"").substr(4-RegExp.$1.length));
}
if(/(E+)/.test(fmt)){
fmt=fmt.replace(RegExp.$1,((RegExp.$1.length>1)?(RegExp.$1.length>2?"/u661f/u671f":"/u5468"):"")+week[this.getDay()+""]);
}
for(varkino){
if(newRegExp("("+k+")").test(fmt)){
fmt=fmt.replace(RegExp.$1,(RegExp.$1.length==1)?(o[k]):(("00"+o[k]).substr((""+o[k]).length)));
}
}
returnfmt;
},
util_date.prototype={
newToday:function(_today){
this.today=_today;
this.year=_today.getYear()+1900;//当前年份
this.Month_a=_today.getMonth();
this.Month=this.Month_a+1;//当前月份
this.day=_today.getDate();//当前日期
this.date=_today.getDay()==0?7:_today.getDay();//本周第几天因系统会把周日作为第0天
this.Monday="";
this.Sunday="";
this.day_one="";
},
getMonday:function(){
if(this.Monday.length!=0){
returnthis.Monday;
}else{
var_monday=newDate(this.year,this.Month_a,this.day-this.date+1);
this.Monday=_monday;
return_monday;
}
},
getSunday:function(){
if(this.Sunday.length!=0){
returnthis.Sunday;
}else{
var_Sunday=newDate(this.year,this.Month_a,this.day-this.date+7);
this.Sunday=_Sunday;
return_Sunday;
}
},
getPreviousMonday:function(Monday){
var_monday=newDate(Monday.getYear()+1900,Monday.getMonth(),Monday.getDate()-7);
return_monday;
},
getPreviousSunday:function(Monday){
var_Sunday=newDate(Monday.getYear()+1900,Monday.getMonth(),Monday.getDate()-1);
this.Sunday=_Sunday;
return_Sunday;
},
getNextMonday:function(Monday){
var_monday=newDate(Monday.getYear()+1900,Monday.getMonth(),Monday.getDate()+7);
return_monday;
},
getNextSunday:function(Monday){
var_Sunday=newDate(Monday.getYear()+1900,Monday.getMonth(),Monday.getDate()+13);
this.Sunday=_Sunday;
return_Sunday;
}
};
window.util_date=newutil_date();
})();
document.write(window.util_date.getMonday().pattern("yyyy-MM-dd"));
</script>
</head>
<body>
</body>
</html>
如果需要可直接在项目中引入该文件使用window.util_date.getMonday().pattern("yyyy-MM-dd");可获得2017-1-24类型的字符串
window.util_date.newToday("2017-1-1");设置当前日期
PS:这里再为大家推荐几款时间及日期相关工具供大家参考使用:
在线日期/天数计算器:
http://tools.jb51.net/jisuanqi/date_jisuanqi
在线日期计算器/相差天数计算器:
http://tools.jb51.net/jisuanqi/datecalc
在线日期天数差计算器:
http://tools.jb51.net/jisuanqi/onlinedatejsq
Unix时间戳(timestamp)转换工具:
http://tools.jb51.net/code/unixtime
更多关于JavaScript相关内容感兴趣的读者可查看本站专题:《JavaScript时间与日期操作技巧总结》、《JavaScript查找算法技巧总结》、《JavaScript错误与调试技巧总结》、《JavaScript数据结构与算法技巧总结》、《JavaScript遍历算法与技巧总结》及《JavaScript数学运算用法总结》
希望本文所述对大家JavaScript程序设计有所帮助。