js根據(jù)當(dāng)前日期獲取前一周或者后一周等日期
JavaScript中可以使用Date()對(duì)象來獲取日期。
具體的使用方法如下:
創(chuàng)建一個(gè)Date對(duì)象:var now = new Date();
獲取當(dāng)前時(shí)間:var time = now.getTime();
獲取年份:var year = now.getFullYear();
獲取月份:var month = now.getMonth() + 1;
獲取日期:var date = now.getDate();
獲取小時(shí):var hour = now.getHours();
獲取分鐘:var minute = now.getMinutes();
獲取秒鐘:var second = now.getSeconds();
獲取星期:var week = now.getDay();
注意,getMonth()方法返回的月份從0開始,所以需要加1。同時(shí)getDay()返回的是星期幾的數(shù)字表示(0表示星期天),需要使用數(shù)組或switch語(yǔ)句進(jìn)行轉(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);
// 獲取當(dāng)前日期具體時(shí)間
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(); //得到小時(shí)
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 + ' 時(shí)間: ' + hour + ":" + minu + ":" + sec;
return time;
}
//獲取當(dāng)前日期
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()
// 獲取當(dāng)前日期的減7天的時(shí)間
function fun_date(aa) {
var date1 = new Date()
var time1 = date1.getFullYear() + "-" + (date1.getMonth() + 1) + "-" + date1.getDate(); //time1表示當(dāng)前時(shí)間
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)
// 獲取當(dāng)前日期減7天的日期
var nowDate = new Date();
nowDate.setDate(nowDate.getDate() -7);
var date=nowDate.getFullYear()+"-"+(nowDate.getMonth()+1)+"-"+nowDate.getDate()
console.log(date)
// 獲取當(dāng)前時(shí)間減7天的日期與具體時(shí)間點(diǎn)(時(shí)間戳獲?。?
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())
// 獲取當(dāng)前時(shí)間加7天的日期與具體時(shí)間點(diǎn)(時(shí)間戳獲?。?
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獲取當(dāng)前日期以及前n天的日期
這里我為了后續(xù)使用方便,封裝了一個(gè)工具類js文件,且里面參數(shù)沒有直接寫死的
utils.js
const formatNumber = n => {
n = n.toString()
return n[1] ? n : '0' + n
}
// 獲取當(dāng)前日期 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() //獲取當(dāng)月的所有天數(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的時(shí)候調(diào)用即可
const utils = require('../../../utils/util');
data:{
startDate: utils.getTimeLastDate(new Date(), 30), //當(dāng)前日期前30天 要前幾天的數(shù)據(jù)就傳幾
endDate: utils.formatDate(new Date()), // 當(dāng)前日期
}
以上的代碼在計(jì)算超過30天的會(huì)有問題,建議使用下面這個(gè)
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)用(當(dāng)前日期前30天):
getNextDate(new Date(), -30)
總結(jié)
到此這篇關(guān)于js根據(jù)當(dāng)前日期獲取前一周或者后一周等日期的文章就介紹到這了,更多相關(guān)js獲取前一周或后一周日期內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- js獲取當(dāng)前周、上一周、下一周日期
- javascript 當(dāng)前日期加(天、周、月、年)
- js實(shí)現(xiàn)獲取當(dāng)前時(shí)間是本月第幾周的方法
- moment.js輕松實(shí)現(xiàn)獲取當(dāng)前日期是當(dāng)年的第幾周
- JS實(shí)現(xiàn)獲取當(dāng)前所在周的周六、周日示例分析
- 怎么使用js計(jì)算當(dāng)前一周的日期
- JS如何根據(jù)當(dāng)前日期獲取一周所有日期
- moment.js 計(jì)算當(dāng)前一周、一月對(duì)應(yīng)日期的實(shí)例
- 如何用js獲取當(dāng)年周數(shù)列表以及當(dāng)前日期是第幾周
相關(guān)文章
Sortable.js功能強(qiáng)大的JavaScript 拖拽庫(kù)示例詳解
SortableJS 是一個(gè)強(qiáng)大、靈活且易于使用的 JavaScript 庫(kù),適用于各種類型的拖放排序需求,這篇文章主要介紹了Sortable.js功能強(qiáng)大的JavaScript 拖拽庫(kù)示例詳解,需要的朋友可以參考下2025-01-01
webpack 如何同時(shí)輸出壓縮和未壓縮的文件的實(shí)現(xiàn)步驟
這篇文章主要介紹了webpack 如何同時(shí)輸出壓縮和未壓縮的文件的實(shí)現(xiàn)步驟,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-06-06
JavaScript實(shí)現(xiàn)表單驗(yàn)證示例
這篇文章主要為大家詳細(xì)介紹了JavaScript實(shí)現(xiàn)表單驗(yàn)證示例,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-06-06
使用layui的router來進(jìn)行傳參的實(shí)現(xiàn)方法
今天小編就為大家分享一篇使用layui的router來進(jìn)行傳參的實(shí)現(xiàn)方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2019-09-09
JavaScript判斷textarea值是否為空并給出相應(yīng)提示
假如用戶沒有輸入數(shù)據(jù)則給出相應(yīng)提示,那么該如何來判斷呢?下面以判斷textarea值是否為空為例2014-09-09
一個(gè)多瀏覽器支持的背景變暗的div并可拖動(dòng)提示窗口功能的代碼
兼容IE、Firefox、Opera前幾天在網(wǎng)上找了許多資料,看了不少兄弟的源碼,一直找不到合適的,要不就是拖動(dòng)有問題,要不就是不兼容Firefox,所以自已寫了一個(gè),下面是代碼:2008-04-04
Javascript的構(gòu)造函數(shù)和constructor屬性
我們知道,默認(rèn)情況下,對(duì)一個(gè)函數(shù)前面使用new,可以構(gòu)造出一個(gè)對(duì)象。每一個(gè)對(duì)象都有一個(gè)constructor屬性,這個(gè)constructor屬性指向構(gòu)造出該對(duì)象的函數(shù)。2010-01-01

