当前位置: 代码网 > it编程>编程语言>Javascript > js根据当前日期获取前一周或者后一周等日期

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

2024年05月26日 Javascript 我要评论
javascript中可以使用date()对象来获取日期。具体的使用方法如下:创建一个date对象:var now = new date();获取当前时间:var time = now.gettime

javascript中可以使用date()对象来获取日期。

具体的使用方法如下:

  • 创建一个date对象: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();

注意,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获取前一周或后一周日期内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!

(0)

相关文章:

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论

验证码:
Copyright © 2017-2025  代码网 保留所有权利. 粤ICP备2024248653号
站长QQ:2386932994 | 联系邮箱:2386932994@qq.com