JS實現(xiàn)獲取當(dāng)前所在周的周六、周日示例分析
本文實例講述了JS實現(xiàn)獲取當(dāng)前所在周的周六、周日。分享給大家供大家參考,具體如下:
需求:無論當(dāng)前是哪一天,獲取當(dāng)天所在周的周末 是哪一天
實現(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,就行,因為很有可能改周末不在同一個月份,比如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é)運算用法總結(jié)》
希望本文所述對大家JavaScript程序設(shè)計有所幫助。
相關(guān)文章
使用純javascript實現(xiàn)經(jīng)典掃雷游戲
本文給大家分享的是個人剛開始學(xué)習(xí)javascript的時候?qū)懙姆聎indows經(jīng)典的掃雷游戲的代碼,當(dāng)時只是寫了下來,沒加注釋,這里補上,有需要的小伙伴可以參考下。2015-04-04JavaScript?中的數(shù)據(jù)類型Number
這篇文章主要介紹了JavaScript?中的數(shù)據(jù)類型Number,Number?類型使用?IEEE?754?格式表示整數(shù)和浮點值,下文相關(guān)詳細(xì)資料介紹,需要的小伙伴可以參考一下2022-04-04JavaScript表格隔行變色和Tab標(biāo)簽頁特效示例【附j(luò)Query版】
這篇文章主要介紹了JavaScript表格隔行變色和Tab標(biāo)簽頁特效,結(jié)合實例形式分析了javascript元素遍歷、事件響應(yīng)相關(guān)操作技巧,并附帶jQuery對應(yīng)實現(xiàn)代碼供大家參考,需要的朋友可以參考下2019-07-07