javascript技巧

关注公众号 jb51net

关闭
首页 > 网络编程 > JavaScript > javascript技巧 > js获取前一周或后一周日期

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

作者:可爱的秋秋啊

有的时候要获取当前日期,或者前一天、后一天的日期,下面这篇文章主要给大家介绍了关于js根据当前日期获取前一周或者后一周等日期的相关资料,文中通过代码介绍的非常详细,需要的朋友可以参考下

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

具体的使用方法如下:

注意,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())

附:javascript获取当前日期以及前n天的日期

这里我为了后续使用方便,封装了一个工具类js文件,且里面参数没有直接写死的

utils.js

const formatNumber = n => {
  n = n.toString()
  return n[1] ? n : '0' + n
}
// 获取当前日期 yyy-mm-dd
const formatDate = date => {
  const year = date.getFullYear()
  const month = date.getMonth() + 1
  const day = date.getDate()

  return [year, month, day].map(formatNumber).join('-')
}
// 获取前n天的日期 last为要求的哪一天,lastDate为哪一天的前n天
const getTimeLastDate = (last,lastDate) => {
  const year = last.getFullYear()
  const day = last.getDate()
  const ti = day - lastDate
  // const month6 = last.getMonth() + 1
  // const dayOfWeek = last.getDay() //今天本周的第几天  
  // 判断是否月初
  if (ti <= 0) {
    const month = last.getMonth() + 1 - 1
    const d = new Date(year, month, 0)
    const dayBig = d.getDate() //获取当月的所有天数
    const ti1 = dayBig + ti
    return [year, month, ti1].map(formatNumber).join('-')
  } else {
    const month = last.getMonth() + 1
    return [year, month, ti].map(formatNumber).join('-')
  }
  // return [year, month, day].map(formatNumber).join('-')

}

module.exports = {
  formatDate: formatDate,
  getTimeLastDate: getTimeLastDate
}

使用(以在小程序 中为例),

直接在声明data的时候调用即可

const utils = require('../../../utils/util');
data:{
    startDate: utils.getTimeLastDate(new Date(), 30), //当前日期前30天 要前几天的数据就传几
    endDate: utils.formatDate(new Date()), // 当前日期
}

以上的代码在计算超过30天的会有问题,建议使用下面这个

  getNextDate(date, day) {
    var dd = new Date(date);
    dd.setDate(dd.getDate() + day);
    var y = dd.getFullYear();
    var m = dd.getMonth() + 1 < 10 ? "0" + (dd.getMonth() + 1) : dd.getMonth() + 1;
    var d = dd.getDate() < 10 ? "0" + dd.getDate() : dd.getDate();
    return y + "-" + m + "-" + d;
  },

调用(当前日期前30天):

getNextDate(new Date(), -30)

总结 

到此这篇关于js根据当前日期获取前一周或者后一周等日期的文章就介绍到这了,更多相关js获取前一周或后一周日期内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

您可能感兴趣的文章:
阅读全文