国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 開發(fā) > JS > 正文

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

2024-05-06 16:50:45
字體:
供稿:網(wǎng)友

本文實(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í)長(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ù)日期獲取幾月幾日

// staurdaylet _satYear = _furtureSaturdayTimes.getFullYear();let _satMonth = _furtureSaturdayTimes.getMonth() + 1;let _satDay = _furtureSaturdayTimes.getDate();//sundaylet _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;}

 

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


注:相關(guān)教程知識(shí)閱讀請(qǐng)移步到JavaScript/Ajax教程頻道。
發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 明光市| 濉溪县| 盐津县| 长泰县| 西昌市| 河东区| 阿城市| 临沭县| 康平县| 霍山县| 岳普湖县| 舞钢市| 吕梁市| 双城市| 宜宾市| 阿城市| 桂平市| 建始县| 海门市| 潼南县| 楚雄市| 宾阳县| 棋牌| 同江市| 浦东新区| 祁门县| 凤台县| 抚顺县| 南安市| 虞城县| 车致| 松溪县| 南岸区| 雷波县| 天长市| 东兰县| 塔城市| 灌南县| 太原市| 新竹市| 南汇区|