js獲取當(dāng)前日期昨天、今天、明天日期的不同方法總結(jié)
一、方法一
1. 獲取當(dāng)前日期
var today = new Date();
2. 獲取昨天的日期
var yesterday = this.getDay(-1)
3. 獲取今天的日期
var today = this.getDay(0)
5. 獲取明天的日期
var tomorrow = this.getDay(1)
6. 調(diào)用的方法示例代碼如下所示:
獲取當(dāng)前日期昨天、今天、明天的日期
methods: {
getDay(day) {
var today = new Date();
var targetday_milliseconds = today.getTime() + 1000 * 60 * 60 * 24 * day;
today.setTime(targetday_milliseconds); //注意,這行是關(guān)鍵代碼
var tYear = today.getFullYear();
var tMonth = today.getMonth();
var tDate = today.getDate();
tMonth = this.doHandleMonth(tMonth + 1);
tDate = this.doHandleMonth(tDate);
return tYear + "-" + tMonth + "-" + tDate;
},
doHandleMonth(month) {
var m = month;
if (month.toString().length == 1) {
m = "0" + month;
}
return m;
},
},
二、方法二
1. 獲取當(dāng)前日期
var today = new Date();
2. 獲取昨天的日期
today .setTime(day1.getTime()-24*60*60*1000); var yesterday = today .getFullYear()+"-" + (today .getMonth()+1) + "-" + today .getDate();
3. 獲取今天的日期
today .setTime(today .getTime()); var day= today .getFullYear()+"-" + (today .getMonth()+1) + "-" + today .getDate();
4. 獲取明天的日期
today .setTime(today .getTime()+24*60*60*1000); var tomorrow= today .getFullYear()+"-" + (today .getMonth()+1) + "-" + today .getDate();
總結(jié):
知識(shí)小結(jié):
總結(jié):
- 獲取當(dāng)前日期并進(jìn)行計(jì)算想要的日期
{
text: '本月',
onClick(picker) {
// 獲取當(dāng)前日期
const today = new Date();
// 獲取當(dāng)前月份的第一天
const start = new Date(today.getFullYear(), today.getMonth(), 1);
// 獲取當(dāng)前月份的最后一天
const end = new Date(today.getFullYear(), today.getMonth() + 1, 0);
picker.$emit('pick', [start, end]);
}
},
- 1、傳值調(diào)用此方法
created() {
console.log("昨天:", this.getDay(-1))
console.log("今天:", this.getDay(0))
console.log("明天:", this.getDay(1))
console.log("20年以后:", this.getDay(20 * 365))
}
- 獲取當(dāng)前時(shí)間, new Date()
- day為number,getDay(-1):昨天的日期;getDay(0):今天的日期;getDay(1):明天的日期;
附:js獲取當(dāng)前星期幾的方法
JavaScript中的Date對(duì)象提供了獲取當(dāng)前日期和時(shí)間的方法。其中,getDay()方法可以返回當(dāng)前星期,返回值為0-6,分別代表星期日到星期六。
以下是使用Date對(duì)象獲取當(dāng)前星期幾的示例代碼:
const weekDays = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'];
const today = new Date();
const dayOfWeek = weekDays[today.getDay()];
console.log(`Today is ${dayOfWeek}`);
在上面的代碼中,我們首先定義了一個(gè)包含星期日到星期六的數(shù)組weekDays。然后,我們創(chuàng)建了一個(gè)Date對(duì)象,使用getDay()方法獲取當(dāng)前星幾,并使用數(shù)組weekDays獲取對(duì)應(yīng)的星期幾名稱。
到此這篇關(guān)于js獲取當(dāng)前日期昨天、今天、明天日期的不同方法的文章就介紹到這了,更多相關(guān)js獲取當(dāng)前日期昨天、今天、明天內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
JSON.parse處理非標(biāo)準(zhǔn)Json數(shù)據(jù)出錯(cuò)的解決
這篇文章主要介紹了JSON.parse處理非標(biāo)準(zhǔn)Json數(shù)據(jù)出錯(cuò)的解決,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-09-09
javascript面向?qū)ο笕筇卣髦鄳B(tài)實(shí)例詳解
這篇文章主要介紹了javascript面向?qū)ο笕筇卣髦鄳B(tài),結(jié)合實(shí)例形式詳細(xì)分析了javascript面向?qū)ο蟪绦蛟O(shè)計(jì)中多態(tài)的概念、原理,并結(jié)合實(shí)例形式總結(jié)了多態(tài)的實(shí)現(xiàn)方法與使用技巧,需要的朋友可以參考下2019-07-07
JavaScript自定義Promise實(shí)現(xiàn)流程
現(xiàn)在網(wǎng)上有非常多的Promise文章,但都是給你一堆代碼,或者某些核心代碼,讓你看完之后感覺(jué),嗯,很厲害,但還是不知所云,不知其所以然。那么本文真正從一個(gè)小白開始帶你深入淺出,一步一步實(shí)現(xiàn)自己的?Promise,這種自己造輪子的過(guò)程一定是進(jìn)步最快的過(guò)程,快上車開始吧2022-10-10
JavaScript 點(diǎn)擊觸發(fā)復(fù)制功能實(shí)例詳解
這篇文章主要介紹了JavaScript 點(diǎn)擊觸發(fā)復(fù)制功能實(shí)例詳解,需要的朋友可以參考下2018-11-11
JavaScript同步與異步任務(wù)問(wèn)題詳解
這篇文章主要介紹了JavaScript事件循環(huán)同步任務(wù)與異步任務(wù),文章圍繞主題展開詳細(xì)的內(nèi)容介紹,具有一定的參考價(jià)值,需要的朋友可以參考一下2022-07-07
JS回調(diào)函數(shù) callback的理解與使用案例分析
這篇文章主要介紹了JS回調(diào)函數(shù) callback的理解與使用,結(jié)合具體案例形式分析了javascript回調(diào)函數(shù)的功能、原理、用法及相關(guān)操作注意事項(xiàng),需要的朋友可以參考下2019-09-09
firefox火狐瀏覽器與與ie兼容的2個(gè)問(wèn)題總結(jié)
這幾天遇到幾個(gè)頭疼的火狐與ie兼容問(wèn)題整理下來(lái),希望對(duì)需要的朋友有所幫助。2010-07-07

