日期和时间戳的格式化以及时间的向前推迟几天和向后推迟几天--first one

日期格式化
<script type="text/javascript">
			//日期格式化
			Date.prototype.Format = function(fmt) {
				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;
			}
			//输出当前时间
			alert(new Date().Format("yyyy-MM-dd"));
			
		</script>
获取时间戳的几种方式以及格式化
<script type="text/javascript">
			// 获得时间戳的几种方式
			var timestamp1 = Date.parse(new Date());
			var timestamp2 = (new Date()).valueOf();
			var timestamp3 = new Date().getTime();
			console.log(timestamp2)
			// 时间戳转正常日期格式
			function timetrans(date) {
				var date = new Date(date); //如果date为13位不需要乘1000
				var Y = date.getFullYear() + '-';
				var M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '-';
				var D = (date.getDate() < 10 ? '0' + (date.getDate()) : date.getDate()) + ' ';
				var h = (date.getHours() < 10 ? '0' + date.getHours() : date.getHours()) + ':';
				var m = (date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes()) + ':';
				var s = (date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds());
				return Y + M + D + h + m + s;
			}
			console.log(timetrans(1553590469530))
		</script>
时间的前后推迟
			//d为天数,提前传负数,推后传正数 0是当天
			function timedelay(now, d) {
				//设置时分秒
				now.setHours(10);
				now.setMinutes(0);
				now.setSeconds(0);
				now.setDate(now.getDate() + d);
				now = now.Format("yyyy-MM-dd hh:mm:ss")//日期格式化函数 上面的
				return now;
			}
			//test
			alert(timedelay(new Date(), -1))//昨天
			alert(timedelay(new Date(), 0))//今天
			alert(timedelay(new Date(), 1))//明天
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值