js获取月份第一天和最后一天,当前天数

js获取月份第一天和最后一天

 <script>
        $(function (){
            var date = new Date();
            var year = date.getFullYear();
            var month = date.getMonth() + 1;
          
            if (month >= 1 && month <= 9) {
                month = "0" + month;
            }
          
            //js 获取当前月份 第一天和最后一天
            var currentdate1 = year+"-"+ month+"-"+ "01";
            var monthEndDate = new Date(year, month, 0);
         
            $("#startDate").val(currentdate1)
            $("#endDate").val(ChangeDateFormat(monthEndDate.getTime(),"yyyy-MM-dd"))
        })
    </script>

由于其他同事已经自定义转换方法ChangeDateFormat(json格式后日期(毫秒数)转成日期格式),直接调用

附上自定义ChangeDateFormat方法

//将序列化成json格式后日期(毫秒数)转成日期格式
function ChangeDateFormat(cellval, type) {
    if (cellval && cellval !='undefined') {
        cellval+="";
        var date = date;
        if (cellval.indexOf("-") == 4)
            date = new Date(cellval);
        else if (cellval.indexOf("/Date(") == -1)
            date = new Date(parseInt(cellval, 10));
        else
            date = new Date(parseInt(cellval.replace("/Date(", "").replace(")/", ""), 10));

        var monthDate = date.getMonth() + 1 < 10 ? "0" + (date.getMonth() + 1) : date.getMonth() + 1;
        var dayDate = date.getDate() < 10 ? "0" + date.getDate() : date.getDate();

        var hoursDate = date.getHours() < 10 ? "0" + date.getHours() : date.getHours();
        var mnuteDate = date.getMinutes() < 10 ? "0" + date.getMinutes() : date.getMinutes();
        var secondsDate = date.getSeconds() < 10 ? "0" + date.getSeconds() : date.getSeconds();

        var strDate = "";
        switch (type) {
            case "yyyy-MM-dd": strDate = date.getFullYear() + "-" + monthDate + "-" + dayDate;
                break;
            case "yy.MM.dd": strDate = date.getFullYear().toString().substr(2,2) + "." + monthDate + "." + dayDate;
                break;
            case "yy-MM-dd": strDate = date.getFullYear().toString().substr(2, 2) + "-" + monthDate + "-" + dayDate;
                break;
            case "yyyy-MM-dd HH:mm": strDate = date.getFullYear() + "-" + monthDate + "-" + dayDate + " " + hoursDate + ":" + mnuteDate;
                break;
            case "MM-dd HH:mm": strDate = monthDate + "-" + dayDate + " " + hoursDate + ":" + mnuteDate;
                break;
            case "yyyy-MM": strDate = date.getFullYear() + "-" + monthDate;
                break;
            case "MM-dd": strDate = monthDate + "-" + dayDate;
                break;
            case "mm-dd": strDate = monthDate + "月" + dayDate + "日";//20190311
                break;
            case "MM-DD": strDate = dayDate + "-" + MonthStr(date.getMonth() + 1);//20190311
                break;
            case "MM-DD-YYYY": strDate = dayDate + "." + MonthStr(date.getMonth() + 1)+date.getFullYear();//20190315
                break;
            case "MM/dd": strDate = monthDate + "/" + dayDate;
                break;
            case "HH:mm:ss": strDate = hoursDate + ":" + mnuteDate + ":" + secondsDate;
                break;
            case "HH:mm": strDate = hoursDate + ":" + mnuteDate;
                break;
            case "HHmm": strDate = hoursDate+""+ mnuteDate;
                break;
            case "yyyyMMddHHmm": strDate = date.getFullYear() + "-" + monthDate + "-" + dayDate + " " + hoursDate + ":" + mnuteDate;
                break;
            default: strDate = date.getFullYear() + "-" + monthDate + "-" + dayDate + " " + hoursDate + ":" + mnuteDate + ":" + secondsDate;
        }
        return strDate;
    }
    else {
        return "";
    }
}

效果

js获取当前日期

 <script>
        $(function (){
            var date = new Date();
            var year = date.getFullYear();
            var month = date.getMonth() + 1;
            var strDate = date.getDate();
            if (month >= 1 && month <= 9) {
                month = "0" + month;
            }
            if (strDate >= 0 && strDate <= 9) {
                strDate = "0" + strDate;
            }

            //js 获取当前月份 当前天数
           
            var currentdate = year+"-"+ month+"-"+ strDate;
            $("#startDate").val(currentdate)
            $("#endDate").val(currentdate)
            
        })
    </script>

效果

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值