本文实例讲述了JS获取本周周一,周末及获取任意时间的周一周末功能。分享给大家供大家参考,具体如下:
项目需要获取本周及任意一天的周一及周末 需格式化,示例代码如下:
/p>
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
星期/**
* @author zhuyangxing
*/
(function() {
function util_date() {
var _today=new Date();
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) {
var o = {
"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() //毫秒
};
var week = {
"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(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;
},
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){
return this.Monday;
}else{
var _monday = new Date(this.year,this.Month_a,this.day-this.date+1);
this.Monday = _monday;
return _monday;
}
},
getSunday:function(){
if(this.Sunday.length!=0){
return this.Sunday;
}else{
var _Sunday = new Date(this.year,this.Month_a,this.day-this.date+7);
this.Sunday = _Sunday;
return _Sunday;
}
},
getPreviousMonday:function(Monday){
var _monday = new Date(Monday.getYear()+1900,Monday.getMonth(),Monday.getDate()-7);
return _monday;
},
getPreviousSunday:function(Monday){
var _Sunday = new Date(Monday.getYear()+1900,Monday.getMonth(),Monday.getDate()-1);
this.Sunday = _Sunday;
return _Sunday;
},
getNextMonday:function(Monday){
var _monday = new Date(Monday.getYear()+1900,Monday.getMonth(),Monday.getDate()+7);
return _monday;
},
getNextSunday:function(Monday){
var _Sunday = new Date(Monday.getYear()+1900,Monday.getMonth(),Monday.getDate()+13);
this.Sunday = _Sunday;
return _Sunday;
}
};
window.util_date = new util_date();
})();
document.write(window.util_date.getMonday().pattern("yyyy-MM-dd"));
如果需要可直接在项目中引入该文件 使用window.util_date.getMonday().pattern("yyyy-MM-dd");可获得2017-1-24类型的字符串
window.util_date.newToday("2017-1-1");设置当前日期
PS:这里再为大家推荐几款时间及日期相关工具供大家参考使用:
希望本文所述对大家JavaScript程序设计有所帮助。