本文實(shí)例講述了JS實(shí)現(xiàn)獲取當(dāng)前所在周的周六、周日。分享給大家供大家參考,具體如下:
創(chuàng)新互聯(lián)建站專注于葉集網(wǎng)站建設(shè)服務(wù)及定制,我們擁有豐富的企業(yè)做網(wǎng)站經(jīng)驗(yàn)。 熱誠(chéng)為您提供葉集營(yíng)銷型網(wǎng)站建設(shè),葉集網(wǎng)站制作、葉集網(wǎng)頁設(shè)計(jì)、葉集網(wǎng)站官網(wǎng)定制、小程序定制開發(fā)服務(wù),打造葉集網(wǎng)絡(luò)公司原創(chuàng)品牌,更為您提供葉集網(wǎng)站排名全網(wǎng)營(yíng)銷落地服務(wù)。
需求:無論當(dāng)前是哪一天,獲取當(dāng)天所在周的周末 是哪一天
實(shí)現(xiàn)步驟:
比如,今天周一,則周日距離今天還有(7-1)=6天,那么將今天的時(shí)間(毫秒數(shù)),加上六天后的時(shí)間(6*_dayLongTime 毫秒數(shù)),然后根據(jù)date函數(shù),轉(zhuǎn)換為幾月幾日。
1、獲取當(dāng)天的時(shí)間
let _nowTime=new Date().getTime();
2、獲取當(dāng)天是星期幾
let _week=_date.getDay();
3、設(shè)置一天的時(shí)長(zhǎng)
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對(duì)象
_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)楹苡锌赡芨闹苣┎辉谕粋€(gè)月份,比如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:這里再為大家推薦幾款時(shí)間及日期相關(guān)工具供大家參考使用:
在線日期/天數(shù)計(jì)算器:
http://tools.jb51.net/jisuanqi/date_jisuanqi
在線日期計(jì)算器/相差天數(shù)計(jì)算器:
http://tools.jb51.net/jisuanqi/datecalc
在線日期天數(shù)差計(jì)算器:
http://tools.jb51.net/jisuanqi/onlinedatejsq
Unix時(shí)間戳(timestamp)轉(zhuǎn)換工具:
http://tools.jb51.net/code/unixtime
更多關(guān)于JavaScript相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《JavaScript時(shí)間與日期操作技巧總結(jié)》、《JavaScript查找算法技巧總結(jié)》、《JavaScript錯(cuò)誤與調(diào)試技巧總結(jié)》、《JavaScript數(shù)據(jù)結(jié)構(gòu)與算法技巧總結(jié)》、《JavaScript遍歷算法與技巧總結(jié)》及《JavaScript數(shù)學(xué)運(yùn)算用法總結(jié)》
希望本文所述對(duì)大家JavaScript程序設(shè)計(jì)有所幫助。