js根據(jù)當前日期獲取前一周或者后一周等日期
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()返回的是星期幾的數(shù)字表示(0表示星期天),需要使用數(shù)組或switch語句進行轉(zhuǎn)換。
示例代碼:
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文件,且里面參數(shù)沒有直接寫死的
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() //獲取當月的所有天數(shù) 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的時候調(diào)用即可
const utils = require('../../../utils/util'); data:{ startDate: utils.getTimeLastDate(new Date(), 30), //當前日期前30天 要前幾天的數(shù)據(jù)就傳幾 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; },
調(diào)用(當前日期前30天):
getNextDate(new Date(), -30)
總結(jié)
到此這篇關(guān)于js根據(jù)當前日期獲取前一周或者后一周等日期的文章就介紹到這了,更多相關(guān)js獲取前一周或后一周日期內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Sortable.js功能強大的JavaScript 拖拽庫示例詳解
SortableJS 是一個強大、靈活且易于使用的 JavaScript 庫,適用于各種類型的拖放排序需求,這篇文章主要介紹了Sortable.js功能強大的JavaScript 拖拽庫示例詳解,需要的朋友可以參考下2025-01-01webpack 如何同時輸出壓縮和未壓縮的文件的實現(xiàn)步驟
這篇文章主要介紹了webpack 如何同時輸出壓縮和未壓縮的文件的實現(xiàn)步驟,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2020-06-06使用layui的router來進行傳參的實現(xiàn)方法
今天小編就為大家分享一篇使用layui的router來進行傳參的實現(xiàn)方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-09-09JavaScript判斷textarea值是否為空并給出相應提示
假如用戶沒有輸入數(shù)據(jù)則給出相應提示,那么該如何來判斷呢?下面以判斷textarea值是否為空為例2014-09-09一個多瀏覽器支持的背景變暗的div并可拖動提示窗口功能的代碼
兼容IE、Firefox、Opera前幾天在網(wǎng)上找了許多資料,看了不少兄弟的源碼,一直找不到合適的,要不就是拖動有問題,要不就是不兼容Firefox,所以自已寫了一個,下面是代碼:2008-04-04Javascript的構(gòu)造函數(shù)和constructor屬性
我們知道,默認情況下,對一個函數(shù)前面使用new,可以構(gòu)造出一個對象。每一個對象都有一個constructor屬性,這個constructor屬性指向構(gòu)造出該對象的函數(shù)。2010-01-01