当前位置: 移动技术网 > IT编程>开发语言>JavaScript > js获取选中日期的当周的周一和周日

js获取选中日期的当周的周一和周日

2018年12月06日  | 移动技术网IT编程  | 我要评论
js获取选中日期的当周的周一和周日 ...

js获取选中日期的当周的周一和周日

 

  

 1 console.log(getnowdateandnowweek(1539187200000));
 2     
 3     /**
 4      * 获取当月的第几周
 5      * a = d = 当前日期
 6      * b = 6 - w = 当前周的还有几天过完(不算今天)
 7      * a + b 的和在除以7 就是当天是当前月份的第几周
 8     */
 9     function getmonthweek(a, b, c) {
10 
11         var date = new date(a, parseint(b) - 1, c), w = date.getday(), d = date.getdate();
12         return math.ceil(
13             (d + 6 - w) / 7
14         );
15     };
16 
17     
18     /**
19      * 获取选择当前的第几周,当前的周一、周日
20      * time 选择日期的时间戳
21      */
22     function getnowdateandnowweek(time) {
23         //选中的时间戳
24         var timestamp = time;
25         var serverdate = new date(time);
26 
27         //本周周日的的时间
28         var sundaytiem = timestamp + ((7 - serverdate.getday()) * 24 * 60 * 60 * 1000)
29         var sundaydata = new date(sundaytiem);
30         //年
31         var tomorrowy = sundaydata.getfullyear();
32         //月
33         var tomorrowm = (sundaydata.getmonth() + 1 < 10 ? '0' + (sundaydata.getmonth() + 1) : sundaydata.getmonth() + 1);
34         //日
35         var tomorrowd = sundaydata.getdate() < 10 ? '0' + sundaydata.getdate() : sundaydata.getdate();
36         console.log('周日:  ' + tomorrowy + '-' + tomorrowm + '-' + tomorrowd);
37 
38         // 本周周一的时间
39         var mondaytime = timestamp - ((serverdate.getday() - 1) * 24 * 60 * 60 * 1000)
40         var mondaydata = new date(mondaytime);
41         //年
42         var mondayy = mondaydata.getfullyear();
43         //月
44         var mondaym = (mondaydata.getmonth() + 1 < 10 ? '0' + (mondaydata.getmonth() + 1) : mondaydata.getmonth() + 1);
45         //日
46         var mondayd = mondaydata.getdate() < 10 ? '0' + mondaydata.getdate() : mondaydata.getdate();
47         var nowweek = getmonthweek(tomorrowy, tomorrowm, tomorrowd);
48         //输出值
49         var config = {
50             sunday: tomorrowy + '/' + tomorrowm + '/' + tomorrowd,
51             monday: mondayy + '/' + mondaym + '/' + mondayd,
52             nowweek: nowweek
53         }
54         return config;
55     }

  

 

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

相关文章:

验证码:
移动技术网