bootstrap time control and time conversion

Set the current time and convert to year-month-day based on milliseconds

    //设置日期默认当天
    function timenow() {
        var timenow = new Date();
        var y = timenow.getFullYear();//年
        var m = timenow.getMonth() + 1;//月
        m = m < 10 ? '0' + m : m;
        var d = timenow.getDate();//日
        d = d < 10 ? ('0' + d) : d;
        var time = y + '-' + m + '-' + d;//字符串拼接成自己想要的时间格式,现在是yyyy/mm/dd格式
        return time;
    }   
    var basePgDate = timenow();

   //设置时间控件的时间为30天前
    monthnow : function () {
        var timenow = new Date();
        timenow.setDate(timenow.getDate() - 30);
        var y = timenow.getFullYear();//年
        var m = timenow.getMonth() + 1;//月
        m = m < 10 ? '0' + m : m;
        var d = timenow.getDate();//日
        d = d < 10 ? ('0' + d) : d;
        var time = y + '-' + m + '-' + d;//字符串拼接成自己想要的时间格式,现在是yyyy/mm/dd格式
        return time;
    },

    //根据毫秒转换成年月日时分秒格式   
    function getMyDate(str){  
        var oDate = new Date(str),  
        oYear = oDate.getFullYear(),  
        oMonth = oDate.getMonth()+1,  
        oDay = oDate.getDate(),  
        oHour = oDate.getHours(),  
        oMin = oDate.getMinutes(),  
        oSen = oDate.getSeconds(),  
        oTime = oYear +'-'+ getzf(oMonth) +'-'+ getzf(oDay) +' '+ getzf(oHour) +':'+ getzf(oMin) +':'+getzf(oSen);//最后拼接时间  
        return oTime;  
    };

    //显示补0操作  
    function getzf(num){  
        if(parseInt(num) < 10){  
            num = '0'+num;  
        }  
        return num;  
    }  

    console.log($.now())  //1505376532620
    console.log(getMyDate($.now())) //2017-09-14 16:09:48
    <link href="https://cdn.bootcss.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet">
    <link rel = "stylesheet" type="text/css" href = "./assets/datetimepicker/css/bootstrap-datetimepicker.css" / >
    <style>
        #ipt::after {
            content: "222";
            display: block;
            width: 5px;
            height: 8px;
            color: red;
            outline: none;
        }

        * {
            box-sizing: border-box;
        }

        #box {
            width: 600px;
            margin: 0 auto;
            padding: 35px;
        }

        #box2 {
            height: 350px;
            border: 1px solid #ccc;
            overflow-y: auto;
            padding: 30px;
        }

        #box2 p {
            margin: 0 auto;
            text-align: center;
        }

        #top_nav {
            position: fixed;
            top: 5px;
            left: 50%;
            width: 200px;
            margin-left: -100px;
        }

        input.datetimepicker {
            margin-top: 10px;
            outline: none;
            border: 1px solid #ccc;
        }
    </style>
</head>
<body>
    <div id="box">
        <p id="top_nav">添加时间控件 <button type="button" id="btn2">添加</button></p>
        <div id="box2"></div>
    </div>
    <script src="https://cdn.bootcss.com/jquery/2.2.3/jquery.min.js"></script>
    <script src="https://cdn.bootcss.com/bootstrap/3.3.7/js/bootstrap.js"></script>
    <script type="text/javascript" src="./assets/datetimepicker/js/bootstrap-datetimepicker.min.js"></script>
    <script type="text/javascript" src="./assets/datetimepicker/js/locales/bootstrap-datetimepicker.zh-CN.js"></script>
        $(function () {
            var timeNum = 1;
            var addTime =
                '<p class="time-p"><input type="text" value="2017-09-10" class="datetimepicker"><span>&nbsp;&nbsp;选择时间</span></p>';
            $("#btn2").on("click", function () {
                var $addTime = $(addTime).children().attr("id", "time" + timeNum).end()
                $("#box2").append($addTime);
                activeTime(("#time" + timeNum));
                timeNum++;
            })

            function activeTime(tagid) {
                $(tagid).datetimepicker({
                    format: 'yyyy-mm-dd',
                    language: 'zh-CN',
                    weekStart: 1,
                    bootcssVer: 3,//箭头样式
                    todayBtn: 1,
                    autoclose: 1,
                    todayHighlight: true,
                    startView: 2,
                    minView: 2,
                    pickerPosition: 'bottom-right',//设置显示位置
                    forceParse: 0,
                    keyboardNavigation: 1,
                    forceParse: 1
                });
            }
            activeTime("#time1")
        })

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325568810&siteId=291194637