js 获取当前时间并格式化

var now = new Date();                                                                                                     
            var year = now.getFullYear(); //得到年份
            var month = now.getMonth();//得到月份
            var date = now.getDate();//得到日期
            month = month + 1;
            if (month < 10) month = "0" + month;
            if (date < 10) date = "0" + date;
            var time = year + "-" + month + "-" + date;    //(格式化"yyyy-MM-dd")     


//input 输入框初始化
<input type="date" id="data">

<script type="text/javascript">
        $(function () {
            data.value = time ;
        })
 </script>                                                                                                               

 

                                                                                                                                        
https://github.com/jacwright/date.format 通过这个地址下载data.format.js 
在js页面引用

//用法
var myDate = new Date();
alert(myDate.format('Y-m-d')); // Outputs "2020-01-01"
alert(myDate.format('Y-m-d H:i:s')); // Outputs "2020-01-01 15:24:30"
alert(myDate.format('M jS, Y')); // Outputs "Nov 26th, 2017"
alert(myDate.format('\\T\\o\\d\\a\\y \\i\\s d-m-Y')); // Outputs "Today is 26-11-2017"

 

posted @ 2020-09-01 15:37  _情书  阅读(7877)  评论(0编辑  收藏  举报