js 日期

转载自:http://blog.csdn.net/u014079773/article/details/52411299


js方法将系统时间转换为字符串案例:

[html]  view plain  copy
  1. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">  
  2. <html>  
  3. <head>  
  4. <meta http-equiv="Content-Type" content="text/html; charset=utf-8">  
  5. <title>js将当前时间转换为字符串日期</title>  
  6. <script type="text/javascript" src="jquery-1.11.1.min.js"></script>  
  7. <script type="text/javascript">  
  8.     $(function () {  
  9.        $("#clickDate").click(function () {  
  10.            var date=new Date();  
  11.            alert("date:"+date);  
  12.            dateStr=dateToString(date);  
  13.            alert("dateStr:"+dateStr);  
  14.        });  
  15.   
  16.     });  
  17. </script>  
  18. <script type="text/javascript">  
  19.   function dateToString(now){  
  20.     var year = now.getFullYear();  
  21.     var month =(now.getMonth() + 1).toString();  
  22.     var day = (now.getDate()).toString();  
  23.     var hour = (now.getHours()).toString();  
  24.     var minute = (now.getMinutes()).toString();  
  25.     var second = (now.getSeconds()).toString();  
  26.     if (month.length == 1) {  
  27.         month = "0" + month;  
  28.     }  
  29.     if (day.length == 1) {  
  30.         day = "0" + day;  
  31.     }  
  32.     if (hour.length == 1) {  
  33.         hour = "0" + hour;  
  34.     }  
  35.     if (minute.length == 1) {  
  36.         minute = "0" + minute;  
  37.     }  
  38.     if (second.length == 1) {  
  39.         second = "0" + second;  
  40.     }  
  41.      var dateTime = year + "-" + month + "-" + day +" "+ hour +":"+minute+":"+second;  
  42.      return dateTime;  
  43.   }  
  44. </script>  
  45. </head>  
  46. <body>  
  47.      <button  id="clickDate">系统当前时间转换为字符串</button>  
  48. </body>  
  49. </html>  


备注:在实际开发中可以获得2个时间进行比较,时间必须已字符串的形式,否则无法进行比较
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值