美文网首页
js获取当前时间并格式化

js获取当前时间并格式化

作者: Mracale | 来源:发表于2020-08-17 09:43 被阅读0次
<!DOCTYPE html>
<html>

<head>
<meta charset="UTF-8">
<title>js获取当前时间并格式化</title>
<link rel="stylesheet" href="https://cdn.staticfile.org/twitter-bootstrap/3.3.7/css/bootstrap.min.css">  
<script src="https://cdn.staticfile.org/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdn.staticfile.org/twitter-bootstrap/3.3.7/js/bootstrap.min.js"></script>
<script>
    /**************************************时间格式化处理************************************/
    function dateFtt(fmt, date) { //author: meizz   
        var o = {
            "M+": date.getMonth() + 1, //月份   
            "d+": date.getDate(), //日   
            "h+": date.getHours(), //小时   
            "m+": date.getMinutes(), //分   
            "s+": date.getSeconds(), //秒   
            "q+": Math.floor((date.getMonth() + 3) / 3), //季度   
            "S": date.getMilliseconds() //毫秒   
        };
        if(/(y+)/.test(fmt))
            fmt = fmt.replace(RegExp.$1, (date.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;
    }
    function time() {
        var myDate = new Date(); //获取系统当前时间
        var a = $("#nowtime").val();
        console.log(a);
        $("#nowtime").val(dateFtt("yyyy-MM-dd hh:mm:ss", myDate));
    }
    //时间计数器
    setInterval("time()",1000);
    </script>
</head>

<body>
    <input type="text" id="nowtime" value="111" />
</body>
</html>

相关文章

网友评论

      本文标题:js获取当前时间并格式化

      本文链接:https://www.haomeiwen.com/subject/ncmfjktx.html