zoukankan      html  css  js  c++  java
  • js-根据日期获取本年所有周日

        /**
         * 
            方法	 描述
            Date()	  返回当日的日期和时间。
            getDate()	从 Date 对象返回一个月中的某一天 (1 ~ 31)。
            getDay()	从 Date 对象返回一周中的某一天 (0 ~ 6)。
            getMonth()	从 Date 对象返回月份 (0 ~ 11)。
            getFullYear()	从 Date 对象以四位数字返回年份。
    
         * @param {*} t 传入的日期 eg:2020-06-31
         */
        getAllSunday(t) {
            let time = t.replace(/-/g, ":");
            time = time.split(":");
            let myTime = new Date(time[0], (time[1] - 1), time[2]);
    
            let date = myTime;
    
            //无参数传入,获取当年的每个周日
            // let date = new Date();
    
            let year = date.getFullYear();
            let m, d, day, dayNum = "";
            let result = "";
    
            for (m = 1; m <= 12; m++) {
                switch (m) {
                    case 1:
                    case 3:
                    case 5:
                    case 7:
                    case 8:
                    case 10:
                    case 12:
                        dayNum = 31;
                        break;
    
                    case 4:
                    case 6:
                    case 9:
                    case 11:
                        dayNum = 30;
                        break;
                    case 2:
                        if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0) {
                            dayNum = 29;
                        } else {
                            dayNum = 28;
                        }
                        break;
                }
    
                for (d = 1; d <= dayNum; d++) {
                    date.setMonth(m - 1, d);
                    //返回表示星期的某一天的数字  0 --> 周日
                    day = date.getDay();
    
                    if (day == 0) {
                        let month = date.getMonth() + 1;
                        let day = date.getDate();
                        if (month < 10) {
                            month = '0' + month;
                        }
                        if (day < 10) {
                            day = '0' + day;
                        }
    
                        result += +date.getFullYear() + '-' + month + '-' + day + ','
                    }
    
                }
            }
        }
    

      

    作者: 莯汐

    出处: < http://www.cnblogs.com/Eileen-lu/>

    本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在页面明显位置给出原文链接。

  • 相关阅读:
    用python爬虫抓站的一些技巧总结
    使用python爬虫抓站的一些技巧总结:进阶篇
    Python模块学习:threading 多线程控制和处理
    Redis操作命令总结
    Redis介绍
    linux内核设计与实现笔记 进程调度
    Python常见数据结构整理
    Linux进程调度原理
    Python yield
    Qt之布局管理器
  • 原文地址:https://www.cnblogs.com/Eileen-lu/p/13633275.html
Copyright © 2011-2022 走看看