獲取本週週一,週末 獲取任意時間的週一週末

項目需要獲取本週及任意一天的週一及週末 需格式化

/**
 * @author zhuyangxing
 * @createdate 2014-1-24
 */
(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();
})();

如果需要可直接在項目中引入該文件 使用window.util_date.getMonday().pattern("yyyy-MM-dd");可獲得2014-1-24類型的字符串

window.util_date.newToday("2014-1-1");設置當前日期

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章