溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

JS實(shí)現(xiàn)獲取當(dāng)前所在周的周六、周日示例分析

發(fā)布時(shí)間:2020-10-02 05:16:23 來源:腳本之家 閱讀:455 作者:zhy前端攻城獅 欄目:web開發(fā)

本文實(shí)例講述了JS實(shí)現(xiàn)獲取當(dāng)前所在周的周六、周日。分享給大家供大家參考,具體如下:

需求:無論當(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í)長

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)楹苡锌赡芨闹苣┎辉谕粋€(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é)》

希望本文所述對大家JavaScript程序設(shè)計(jì)有所幫助。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI