js自动计算给定日期所在周的周一与周日对应的日期

<script type="text/javascript">
		$(function(){
			
			String.prototype.format = function() {
				var vs = arguments;
				return this.replace(/\{(\d+)\}/g, function() { return vs[parseInt(arguments[1])]; });
			};
			
			Date.prototype.format = function(formatString) {
				with (this) {
					return (formatString||"{0}-{1}-{2} {3}:{4}:{5}").format(
						  getFullYear()
						, ("0" + (getMonth()+1)).slice(-2)
						, ("0" + getDate()).slice(-2)
						, ("0" + getHours()).slice(-2)
						, ("0" + getMinutes()).slice(-2)
						, ("0" + getSeconds()).slice(-2)
					);
				}
			};
			
			function getWeek(theDay) {
				var monday = new Date(theDay.getTime());
				var sunday = new Date(theDay.getTime());
				monday.setDate(monday.getDate()+1-monday.getDay());
				sunday.setDate(sunday.getDate()+7-sunday.getDay());
				return {monday:monday, sunday:sunday};
			}
			
			
			var week = getWeek(new Date('2014/1/4'));
			alert(
				week.monday.format("{0}.{1}.{2}")
				+ " - " +
				week.sunday.format("{0}.{1}.{2}")
			);

	})
	</script>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值