js获取当前的年月日时分秒

2022-12-04  本文已影响0人  祈澈菇凉
<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <title></title>

    </head>

    <body>
        <input type="text" class="form-control input-medium" placeholder="开始时间" name="" id="startTime" />
        <input type="text" class="form-control input-medium" placeholder="结束时间" name="" id="endTime" />

    </body>
    <script type="text/javascript">
        //   var mydateInputbeginDate = document.getElementById("startTime");
        //         var datebeginDate = new Date();
        //         datebeginDate.setMonth(datebeginDate.getMonth()-1);

        //         //下面语句判断,当月份小于10月时,前面位0
        //         var YbeginDate = datebeginDate.getFullYear();
        //         var MbeginDate = datebeginDate.getMonth() + 1;

        //         if( MbeginDate < 10){
        //             MbeginDate = '0' + MbeginDate;
        //             }
        //         var DbeginDate = datebeginDate.getDate();
        //         if( DbeginDate < 10){
        //             DbeginDate = '0' + DbeginDate;
        //             }
        //         var dateStringbeginDate = YbeginDate + "-" + MbeginDate + "-" + DbeginDate;
        //         mydateInputbeginDate.value = dateStringbeginDate;

        //获取年月日时分秒
        var mydateInputstartTime = document.getElementById("startTime");
        var date = new Date();
        //下面语句判断,当月份小于10月时,前面位0
        var Y = date.getFullYear(); //获取当前年份
        var M = date.getMonth() + 1; //获取当前月份
        var D = date.getDate(); //获取当前日 
        var hour = date.getHours(); //获取当前时
        var min = date.getMinutes(); //获取当前分
        var second = date.getSeconds(); //获取当前秒
        //判断月份
        if (M < 10) {
            M = '0' + M;
        }
        //判断日
        if (D < 10) {
            D = '0' + D;
        }
        //判断小时
        if (hour < 10) {
            hour = '0' + hour;
        }
        //判断分钟   
        if (min < 10) {
            min = '0' + min;
        }
        //判断秒
        if (second < 10) {
            second = '0' + second;
        }
        var dateString =  Y +"-"+ M+ "-"+ D +" "+ hour + ":"+min +":"+ second;
        mydateInputstartTime.value = dateString;



        // var mydateInput = document.getElementById("endTime");
        // var date = new Date();

        // //下面语句判断,当月份小于10月时,前面位0
        // var Y = date.getFullYear();

        // var M = date.getMonth() + 1;

        // if (M < 10) {
        //  M = '0' + M;
        // }

        // var D = date.getDate();
        // if (D < 10) {
        //  D = '0' + D;
        // }


        // var dateString = Y + "-" + M + "-" + D;
        // mydateInput.value = dateString;
    </script>

</html>

上一篇下一篇

猜你喜欢

热点阅读