码迷,mamicode.com
首页 > Web开发 > 详细

js获取当前时间转换时间格式yyyy-mm-dd hh:mm:ss

时间:2018-12-07 00:34:10      阅读:290      评论:0      收藏:0      [点我收藏+]

标签:round   new   seconds   cond   art   参数   date   charset   title   

<!DOCTYPE html>
<html>
    <head>
        <meta charset="{CHARSET}">
        <title></title>
        <script>
            // 第一个参数为日期,第二个参数为年月日分割格式 ‘/‘或‘-‘
            function format(Date,str){
                var obj = {
                    Y: Date.getFullYear(),
                    M: Date.getMonth() + 1,
                    D: Date.getDate(),
                    H: Date.getHours(),
                    Mi: Date.getMinutes(),
                    S: Date.getSeconds()
                }
                // 拼接时间 hh:mm:ss
                var time =   +supplement(obj.H) + : + supplement(obj.Mi) + : + supplement(obj.S);
                // yyyy-mm-dd
                if(str.indexOf(-) > -1){
                    return obj.Y + - + supplement(obj.M) + - + supplement(obj.D) + time;
                }
                // yyyy/mm/dd
                if(str.indexOf(/) > -1){
                    return obj.Y + / + supplement(obj.M) + / + supplement(obj.D) + time;
                }
            }
             
            // 位数不足两位补全0
            function supplement(nn){
                return nn = nn < 10 ? 0 + nn : nn;
            }
             
            var todaydate = new Date();
            var end_time = format(todaydate,-);
            alert(end_time);
            var predata = new Date(todaydate-24*3600*1000);
            var start_time = format(predata,-);
            alert(start_time);
</script> </head> <body> 1212 </body> </html>

 

js获取当前时间转换时间格式yyyy-mm-dd hh:mm:ss

标签:round   new   seconds   cond   art   参数   date   charset   title   

原文地址:https://www.cnblogs.com/juzijiang/p/10080618.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!