js 根据当前获取前一周或者后一周等日期

JavaScript中可以使用Date()对象来获取日期。

具体的使用方法如下:

  1. 创建一个Date对象:var now = new Date();

  2. 获取当前时间:var time = now.getTime();

  3. 获取年份:var year = now.getFullYear();

  4. 获取月份:var month = now.getMonth() + 1;

  5. 获取日期:var date = now.getDate();

  6. 获取小时:var hour = now.getHours();

  7. 获取分钟:var minute = now.getMinutes();

  8. 获取秒钟:var second = now.getSeconds();

  9. 获取星期:var week = now.getDay();

注意,getMonth()方法返回的月份从0开始,所以需要加1。同时getDay()返回的是星期几的数字表示(0表示星期天),需要使用数组或switch语句进行转换。

示例代码:

var now = new Date();
var time = now.getTime();
var year = now.getFullYear();
var month = now.getMonth() + 1;
var date = now.getDate();
var hour = now.getHours();
var minute = now.getMinutes();
var second = now.getSeconds();
var week = now.getDay();
console.log(year, month, date, hour, minute, second, week); 
            // 获取当前日期具体时间
			function getCurrentDate() {
				var now = new Date();
				var year = now.getFullYear(); //得到年份
				var month = now.getMonth(); //得到月份
				var date = now.getDate(); //得到日期
				var day = now.getDay(); //得到周几
				var hour = now.getHours(); //得到小时
				var minu = now.getMinutes(); //得到分钟
				var sec = now.getSeconds(); //得到秒
				month = month + 1;
				if (month < 10) month = "0" + month;
				if (date < 10) date = "0" + date;
				if (hour < 10) hour = "0" + hour;
				if (minu < 10) minu = "0" + minu;
				if (sec < 10) sec = "0" + sec;
				var time = "";
				//精确到天
				time = year + "-" + month + "-" + date + '周' + day + ' 时间: ' + hour + ":" + minu + ":" + sec;
				return time;
			}

			//获取当前日期
			function getCurrentDate1() {
				var startDate = new Date();
				var year = startDate.getFullYear();
				var month = startDate.getMonth() + 1;
				var day = startDate.getDate();
				if (month < 10) month = "0" + month;
				if (day < 10) day = "0" + day;
				return year + '-' + month + '-' + day;
			}
			var a = getCurrentDate()
			var b = getCurrentDate1()
			
			
			// 获取当前日期的减7天的时间
			function fun_date(aa) {
				var date1 = new Date()
				var time1 = date1.getFullYear() + "-" + (date1.getMonth() + 1) + "-" + date1.getDate(); //time1表示当前时间
				var date2 = new Date(date1);
				date2.setDate(date1.getDate() - aa);
				var time2 = date2.getFullYear() + "-" + (date2.getMonth() + 1) + "-" + date2.getDate();
				return time2
			}
			var c = fun_date(7)

			console.log(a, b, c)
			
			// 获取当前日期减7天的日期
			var nowDate = new Date();
			nowDate.setDate(nowDate.getDate() -7);
			var date=nowDate.getFullYear()+"-"+(nowDate.getMonth()+1)+"-"+nowDate.getDate()
			console.log(date)
			
			// 获取当前时间减7天的日期与具体时间点(时间戳获取)
			var start = new Date().getTime()/1000
			var end = start - (60*60*24*7)
			var lastDate=new Date(parseInt(end) * 1000).getFullYear()+"-"+(new Date(parseInt(end) * 1000).getMonth()+1)+"-"+new Date(parseInt(end) * 1000).getDate()
			console.log(lastDate,new Date(parseInt(end) * 1000).toLocaleString())	
			
			// 获取当前时间加7天的日期与具体时间点(时间戳获取)
			var start1 = new Date().getTime()/1000
			var end1 = start + (60*60*24*7)
			var lastDate1=new Date(parseInt(end) * 1000).getFullYear()+"-"+(new Date(parseInt(end) * 1000).getMonth()+1)+"-"+new Date(parseInt(end) * 1000).getDate()
			console.log(lastDate1,new Date(parseInt(end1) * 1000).toLocaleString())

你可能感兴趣的:(js,javascript,前端,开发语言)