js-----Date==>>字符串

  1. <html>  
  2. <head>  
  3. <meta http-equiv="Content-Type" content="text/html; charset=utf-8">  
  4. <title>js将当前时间转换为字符串日期</title>  
  5. <script type="text/javascript" src="jquery-1.11.1.min.js"></script>  
  6. <script type="text/javascript">  
  7.     $(function () {  
  8.        $("#clickDate").click(function () {  
  9.            var date=new Date();  
  10.            alert("date:"+date);  
  11.            dateStr=dateToString(date);  
  12.            alert("dateStr:"+dateStr);  
  13.        });  
  14.   
  15.     });  
  16. </script>  
  17. <script type="text/javascript">  
  18.   function dateToString(now){  
  19.     var year = now.getFullYear();  
  20.     var month =(now.getMonth() + 1).toString();  
  21.     var day = (now.getDate()).toString();  
  22.     var hour = (now.getHours()).toString();  
  23.     var minute = (now.getMinutes()).toString();  
  24.     var second = (now.getSeconds()).toString();  
  25.     if (month.length == 1) {  
  26.         month = "0" + month;  
  27.     }  
  28.     if (day.length == 1) {  
  29.         day = "0" + day;  
  30.     }  
  31.     if (hour.length == 1) {  
  32.         hour = "0" + hour;  
  33.     }  
  34.     if (minute.length == 1) {  
  35.         minute = "0" + minute;  
  36.     }  
  37.     if (second.length == 1) {  
  38.         second = "0" + second;  
  39.     }  
  40.      var dateTime = year + "-" + month + "-" + day +" "+ hour +":"+minute+":"+second;  
  41.      return dateTime;  
  42.   }  
  43. </script>  
  44. </head>  
  45. <body>  
  46.      <button  id="clickDate">系统当前时间转换为字符串</button>  
  47. </body>  
  48. </html>  
posted @ 2017-12-18 18:01  字帖公子  阅读(175)  评论(0编辑  收藏  举报