JS實(shí)現(xiàn)獲取當(dāng)前所在周的周六、周日示例分析
本文實(shí)例講述了JS實(shí)現(xiàn)獲取當(dāng)前所在周的周六、周日。分享給大家供大家參考,具體如下:
需求:無論當(dāng)前是哪一天,獲取當(dāng)天所在周的周末 是哪一天
實(shí)現(xiàn)步驟:
比如,今天周一,則周日距離今天還有(7-1)=6天,那么將今天的時間(毫秒數(shù)),加上六天后的時間(6*_dayLongTime 毫秒數(shù)),然后根據(jù)date函數(shù),轉(zhuǎn)換為幾月幾日。
1、獲取當(dāng)天的時間
let _nowTime=new Date().getTime();
2、獲取當(dāng)天是星期幾
let _week=_date.getDay();
3、設(shè)置一天的時長
let _dayLongTime=24*60*60*1000;
4、獲取周六周日距離現(xiàn)在還有多少毫秒
let _furtureSundayTimes = _nowTime + (7 - _week) * _dayLongTime; let _furtureSaturdayTimes = _nowTime + (6 - _week) * _dayLongTime;
5、將毫秒數(shù)轉(zhuǎn)為date對象
_furtureSundayTimes = new Date(_furtureSundayTimes); _furtureSaturdayTimes = new Date(_furtureSaturdayTimes);
6、根據(jù)日期獲取幾月幾日
// staurday let _satYear = _furtureSaturdayTimes.getFullYear(); let _satMonth = _furtureSaturdayTimes.getMonth() + 1; let _satDay = _furtureSaturdayTimes.getDate(); //sunday let _sunYear = _furtureSundayTimes.getFullYear(); let _sunMonth = _furtureSundayTimes.getMonth() + 1; let _sunDay = _furtureSundayTimes.getDate();
7、格式化
_satMonth = _satMonth >= 10 ? _satMonth : '0' + _satMonth; _satDay = _satDay >= 10 ? _satDay : '0' + _satDay; _sunMonth = _sunMonth >= 10 ? _sunMonth : '0' + _sunMonth; _sunDay = _sunDay >= 10 ? _sunDay : '0' + _sunDay; _mealSunDay = _satYear+'-'+_satMonth+'-'+_satDay; _mealSaturDay = _sunYear+ '-'+_sunMonth+'-'+_sunDay;
8、注:之所以不僅獲取周六,然后周日則用周六加1,就行,因?yàn)楹苡锌赡芨闹苣┎辉谕粋€月份,比如3.31周六,4.01周日,月份不相同
9、方法體
function getWeekDay() {
let _date = new Date();
let _nowTime = _date.getTime();
let _week = _date.getDay();
let _dayLongTime = 24 * 60 * 60 * 1000;
let _furtureSundayTimes = _nowTime + (7 - _week) * _dayLongTime;
let _furtureSaturdayTimes = _nowTime + (6 - _week) * _dayLongTime;
_furtureSundayTimes = new Date(_furtureSundayTimes);
_furtureSaturdayTimes = new Date(_furtureSaturdayTimes);
// staurday
let _satYear = _furtureSaturdayTimes.getFullYear();
let _satMonth = _furtureSaturdayTimes.getMonth() + 1;
let _satDay = _furtureSaturdayTimes.getDate();
//sunday
let _sunYear = _furtureSundayTimes.getFullYear();
let _sunMonth = _furtureSundayTimes.getMonth() + 1;
let _sunDay = _furtureSundayTimes.getDate();
_satMonth = _satMonth >= 10 ? _satMonth : '0' + _satMonth;
_satDay = _satDay >= 10 ? _satDay : '0' + _satDay;
_sunMonth = _sunMonth >= 10 ? _sunMonth : '0' + _sunMonth;
_sunDay = _sunDay >= 10 ? _sunDay : '0' + _sunDay;
_mealSunDay = _satYear+'-'+_satMonth+'-'+_satDay;
_mealSaturDay = _sunYear+ '-'+_sunMonth+'-'+_sunDay;
let _weekendDay = [{
saturDay: _mealSunDay
}, {
sunDay: _mealSaturDay
}]
return _weekendDay;
}
PS:這里再為大家推薦幾款時間及日期相關(guān)工具供大家參考使用:
在線日期/天數(shù)計算器:
http://tools.jb51.net/jisuanqi/date_jisuanqi
在線日期計算器/相差天數(shù)計算器:
http://tools.jb51.net/jisuanqi/datecalc
在線日期天數(shù)差計算器:
http://tools.jb51.net/jisuanqi/onlinedatejsq
Unix時間戳(timestamp)轉(zhuǎn)換工具:
http://tools.jb51.net/code/unixtime
更多關(guān)于JavaScript相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《JavaScript時間與日期操作技巧總結(jié)》、《JavaScript查找算法技巧總結(jié)》、《JavaScript錯誤與調(diào)試技巧總結(jié)》、《JavaScript數(shù)據(jù)結(jié)構(gòu)與算法技巧總結(jié)》、《JavaScript遍歷算法與技巧總結(jié)》及《JavaScript數(shù)學(xué)運(yùn)算用法總結(jié)》
希望本文所述對大家JavaScript程序設(shè)計有所幫助。
- js獲取當(dāng)前周、上一周、下一周日期
- javascript 當(dāng)前日期加(天、周、月、年)
- js實(shí)現(xiàn)獲取當(dāng)前時間是本月第幾周的方法
- moment.js輕松實(shí)現(xiàn)獲取當(dāng)前日期是當(dāng)年的第幾周
- 怎么使用js計算當(dāng)前一周的日期
- JS如何根據(jù)當(dāng)前日期獲取一周所有日期
- moment.js 計算當(dāng)前一周、一月對應(yīng)日期的實(shí)例
- js根據(jù)當(dāng)前日期獲取前一周或者后一周等日期
- 如何用js獲取當(dāng)年周數(shù)列表以及當(dāng)前日期是第幾周
相關(guān)文章
使用純javascript實(shí)現(xiàn)經(jīng)典掃雷游戲
本文給大家分享的是個人剛開始學(xué)習(xí)javascript的時候?qū)懙姆聎indows經(jīng)典的掃雷游戲的代碼,當(dāng)時只是寫了下來,沒加注釋,這里補(bǔ)上,有需要的小伙伴可以參考下。2015-04-04
JavaScript?中的數(shù)據(jù)類型Number
這篇文章主要介紹了JavaScript?中的數(shù)據(jù)類型Number,Number?類型使用?IEEE?754?格式表示整數(shù)和浮點(diǎn)值,下文相關(guān)詳細(xì)資料介紹,需要的小伙伴可以參考一下2022-04-04
純JS實(shí)現(xiàn)可用于頁碼更換的飛頁特效示例
這篇文章主要介紹了純JS實(shí)現(xiàn)可用于頁碼更換的飛頁特效,涉及javascript結(jié)合時間函數(shù)的數(shù)學(xué)運(yùn)算與頁面元素屬性動態(tài)修改相關(guān)操作技巧,需要的朋友可以參考下2018-05-05
JavaScript表格隔行變色和Tab標(biāo)簽頁特效示例【附j(luò)Query版】
這篇文章主要介紹了JavaScript表格隔行變色和Tab標(biāo)簽頁特效,結(jié)合實(shí)例形式分析了javascript元素遍歷、事件響應(yīng)相關(guān)操作技巧,并附帶jQuery對應(yīng)實(shí)現(xiàn)代碼供大家參考,需要的朋友可以參考下2019-07-07
JavaScript實(shí)現(xiàn)簡單的星星評分效果
這篇文章主要為大家詳細(xì)介紹了JavaScript實(shí)現(xiàn)簡單的星星評分效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-05-05

