星空网 > 软件开发 > Java

javascript中日期函数的相关操作

2016-08-03 11:41:10

new Date()
new Date(milliseconds)
new Date(datestring)
new Date(year, month)
new Date(year, month, day)
new Date(year, month, day, hours)
new Date(year, month, day, hours, minutes)
new Date(year, month, day, hours, minutes, seconds)
new Date(year, month, day, hours, minutes, seconds, microseconds)

下面对1.new Date(),没有参数的时候,创建的是当前时间日期对象。2.new Date(milliseconds),当参数为数字的时候,那么这个参数就是时间戳,被视为毫秒,创建一个距离1970年1月一日指定毫秒的时间日期对象。3.new Date(datestring),此参数是一个字符串,并且此字符串一定能够使用Date.parse()转换。

3.new Date(datestring),此参数是一个字符串,并且此字符串一定能够使用Date.parse()转换。
4.以下六个构造函数是精确定义:
  1).year,是一个整数,如果是0-99,那么在此基础上加1900,其他的都原样返回。
  2).month,是一个整数,范围是0-11。
  3).day,是一个整数,范围是1-31。
  4).hours,是一个整数,范围是0-23。
  5).minutes,是一个整数,范围是0-59。
  6).seconds,是一个整数,范围是0-59。
  7).microseconds 是一个整数,范围是0-9999。

 1 <html> 2   <head> 3   <title>时间戳转化为年月日时分秒</title> 4   <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />   5   </head> 6   <body> 7   </body> 8 </html> 9 <script>10   window.onload=function(){11 12      var now=new Date();//当前系统时间 13      var shijianchuo = now.getTime();//获取当前时间戳14      alert("时间戳:"+shijianchuo);15      var nowdate = new Date(shijianchuo);//将时间戳转化为日期对象16      var nowtime=nowdate.Format("yyyy-MM-dd hh:mm:ss");//格式化当前系统时间,相当于将时间戳转化为年月日时分秒了17      alert("当前时间:"+nowtime);18 19    }20 21   22   /*23   日期格式化:24   对Date的扩展,将 Date 转化为指定格式的String25   年(y)可以用1-4个占位符,季度(q)可以用1-2个占位符.26   月(M)、日(d)、小时(h)、分(m)、秒(s)可以用1-2个占位符.27   毫秒(S)只能用1个占位符(是1-3位的数字) 28   例子: 29   (new Date()).Format("yyyy-MM-dd hh:mm:ss.S")30   (new Date()).Format("yyyy-MM-dd hh:mm:ss.S毫秒 第qq季度")31   */32   Date.prototype.Format = function (fmt) { 33     var o = {34       "M+": this.getMonth() + 1, //月 35       "d+": this.getDate(), //日 36       "h+": this.getHours(), //时 37       "m+": this.getMinutes(), //分 38       "s+": this.getSeconds(), //秒 39       "q+": Math.floor((this.getMonth() + 3) / 3), //季度 40       "S": this.getMilliseconds() //毫秒 41     };42     if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));43     for (var k in o)44     if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? 
       (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));45 return fmt;46 }47 </script>

 




原标题:javascript中日期函数的相关操作

关键词:JavaScript

*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。
相关文章
我的浏览记录
最新相关资讯
海外公司注册 | 跨境电商服务平台 | 深圳旅行社 | 东南亚物流