当前位置: 移动技术网 > IT编程>开发语言>JavaScript > JS实现获取当前所在周的周六、周日示例分析

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

2019年07月21日  | 移动技术网IT编程  | 我要评论
本文实例讲述了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 ? _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:这里再为大家推荐几款时间及日期相关工具供大家参考使用:

在线日期/天数计算器:

在线日期计算器/相差天数计算器:

在线日期天数差计算器:

unix时间戳(timestamp)转换工具:

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

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

如您对本文有疑问或者有任何想说的,请 点击进行留言回复,万千网友为您解惑!

相关文章:

验证码:
移动技术网