JS实现获取当前所在周的周六、周日示例分析

网络编程 发布日期:2024/9/24 浏览次数:1

正在浏览:JS实现获取当前所在周的周六、周日示例分析

本文实例讲述了JS实现获取当前所在周的周六、周日。分享给大家供大家参考,具体如下:

需求:无论当前是哪一天,获取当天所在周的周末 是哪一天

实现步骤:

比如,今天周一,则周日距离今天还有(7-1)=6天,那么将今天的时间(毫秒数),加上六天后的时间(6*_dayLongTime 毫秒数),然后根据date函数,转换为几月几日。

1、获取当天的时间

let _nowTime=new Date().getTime();

2、获取当天是星期几

let _week=_date.getDay();

3、设置一天的时长

let _dayLongTime=24*60*60*1000;

4、获取周六周日距离现在还有多少毫秒

let _furtureSundayTimes = _nowTime + (7 - _week) * _dayLongTime;
let _furtureSaturdayTimes = _nowTime + (6 - _week) * _dayLongTime;

5、将毫秒数转为date对象

_furtureSundayTimes = new Date(_furtureSundayTimes);
_furtureSaturdayTimes = new Date(_furtureSaturdayTimes);

6、根据日期获取几月几日

// 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 "htmlcode">
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 "color: #800000">PS:这里再为大家推荐几款时间及日期相关工具供大家参考使用:

在线日期/天数计算器:
http://tools.jb51.net/jisuanqi/date_jisuanqi

在线日期计算器/相差天数计算器:
http://tools.jb51.net/jisuanqi/datecalc

在线日期天数差计算器:
http://tools.jb51.net/jisuanqi/onlinedatejsq

Unix时间戳(timestamp)转换工具:
http://tools.jb51.net/code/unixtime

更多关于JavaScript相关内容感兴趣的读者可查看本站专题:《JavaScript时间与日期操作技巧总结》、《JavaScript查找算法技巧总结》、《JavaScript错误与调试技巧总结》、《JavaScript数据结构与算法技巧总结》、《JavaScript遍历算法与技巧总结》及《JavaScript数学运算用法总结》

希望本文所述对大家JavaScript程序设计有所帮助。

荣耀猎人回归!七大亮点看懂不只是轻薄本,更是游戏本的MagicBook Pro 16.
人们对于笔记本电脑有一个固有印象:要么轻薄但性能一般,要么性能强劲但笨重臃肿。然而,今年荣耀新推出的MagicBook Pro 16刷新了人们的认知——发布会上,荣耀宣布猎人游戏本正式回归,称其继承了荣耀 HUNTER 基因,并自信地为其打出“轻薄本,更是游戏本”的口号。
众所周知,寻求轻薄本的用户普遍更看重便携性、外观造型、静谧性和打字办公等用机体验,而寻求游戏本的用户则普遍更看重硬件配置、性能释放等硬核指标。把两个看似难以相干的产品融合到一起,我们不禁对它产生了强烈的好奇:作为代表荣耀猎人游戏本的跨界新物种,它究竟做了哪些平衡以兼顾不同人群的各类需求呢?