最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

js根據當前日期獲取前一周或者后一周等日期

 更新時間:2024年04月29日 10:14:08   作者:可愛的秋秋啊  
有的時候要獲取當前日期,或者前一天、后一天的日期,下面這篇文章主要給大家介紹了關于js根據當前日期獲取前一周或者后一周等日期的相關資料,文中通過代碼介紹的非常詳細,需要的朋友可以參考下

JavaScript中可以使用Date()對象來獲取日期。

具體的使用方法如下:

  • 創(chuàng)建一個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天的日期

這里我為了后續(xù)使用方便,封裝了一個工具類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獲取前一周或后一周日期內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!

相關文章

最新評論

沅陵县| 兴安县| 苗栗县| 望奎县| 芦山县| 微山县| 京山县| 新平| 延边| 洞口县| 海晏县| 平果县| 桃园县| 长宁县| 南召县| 湛江市| 精河县| 正定县| 枣阳市| 沙坪坝区| 五华县| 沾益县| 诸暨市| 岑溪市| 仙桃市| 来宾市| 金阳县| 报价| 甘泉县| 濉溪县| 刚察县| 筠连县| 南平市| 榆社县| 武强县| 乌恰县| 元谋县| 弋阳县| 元朗区| 武隆县| 塘沽区|