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


    刚找这个的js方法,找到了下面的博客:

    https://www.cnblogs.com/nelsonlei/p/10071659.html

    不知道为什么要把东西写的这么复杂。。。。

    我自己写了个:

    function getDate(type,date){
        let d=date || new Date()
        let s=d.format("yyyy-MM-dd")
        if(type=="当天"){
            return [s,s]
        }else if(type=="本周"){
            let t=d.getTime()
            let day=d.getDay()
            day=day==0?7:day
            let d1= t-(day-1)*24*60*60*1000
            let d2= t+(7-day)*24*60*60*1000
            return [new Date(d1).format("yyyy-MM-dd"),new Date(d2).format("yyyy-MM-dd")]
        }else if(type=="本月"){
            let arr=s.split("-")
            return [arr[0]+"-"+arr[1]+"-01",arr[0]+"-"+arr[1]+"-"+run_nian(d)]
        }
    }
    
    let max_month=[1,3,5,7,8,10,12]
    function run_nian(d){
        let month=d.getMonth()+1
        if(month==2){
            let year=d.getFullYear()
            if(year % 100 ==0){
                if(year % 400 ==0){
                    return 29
                }
                return 28
            }else{
                return 28
            }
        }else{
            if($.inArray(month,max_month)>=0){
                return 31
            }else{
                return 30
            }
        }
    }
    /**
     * 日期格式化
     * @param {Object} fmt 格式化字符串,例:‘yyyy-MM-dd HH:mm:ss’
     */
    Date.prototype.format = function(fmt) { //author: meizz
        var o = {
            "M+": this.getMonth() + 1, //月份
            "d+": this.getDate(), //
            "H+": this.getHours(), //小时
            "m+": this.getMinutes(), //
            "s+": this.getSeconds(), //
            "q+": Math.floor((this.getMonth() + 3) / 3), //季度
            "S": this.getMilliseconds() //毫秒
        };
        if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
        for (var k in o)
            if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : ((
                "00" + o[k]).substr(("" + o[k]).length)));
        return fmt;
    }

    当然我这个复杂是有三种,单个的比那个简单多了。

  • 相关阅读:
    Hibernate配置
    Log4j 局部笔记
    有关接口 笔记 懒人版
    JAVA面向对象编程这本书的摘录~!(2016-5-23)
    关于关闭数据流
    安卓桌面开发小应用
    ACM Sdut 2158 Hello World!(数学题,排序) (山东省ACM第一届省赛C题)
    hdu 1573 A/B (扩展欧几里得)
    hdu 1788 Chinese remainder theorem again(最小公倍数)
    ACM hdu 1019 Least Common Multiple
  • 原文地址:https://www.cnblogs.com/ztw1122/p/15842232.html
Copyright © 2020-2023  润新知