冷面阎罗

低调做人&&高调做事
随笔 - 208, 文章 - 3, 评论 - 593, 引用 - 0
数据加载中……

javascipt中的时间函数

今天作程序的时候用到了,在javascipt中回去的时间。以前我们都是在java中获取时间然后将时间传到js中,这样不好之处就是当进入页面后,等了好长时间在用时间的话,你传过来的时间就是进入页面的时间。后来就自己编写个类,实现。该类的代码如下
Date.prototype.format = function(format) //author: meizz
{
  var o = {
    "M+" : this.getMonth()+1, //month
    "d+" : this.getDate(),    //day
    "h+" : this.getHours(),   //hour
    "m+" : this.getMinutes(), //minute
    "s+" : this.getSeconds(), //second
    "q+" : Math.floor((this.getMonth()+3)/3),  //quarter
    "S" : this.getMilliseconds() //millisecond
  }
  if(/(y+)/.test(format)) format=format.replace(RegExp.$1,
    (this.getFullYear()+"").substr(4 - RegExp.$1.length));
  for(var k in o)if(new RegExp("("+ k +")").test(format))
    format = format.replace(RegExp.$1,
      RegExp.$1.length==1 ? o[k] :
        ("00"+ o[k]).substr((""+ o[k]).length));
  return format;
}
然后你要到的话只要这样就可以,new Date().format("yyyy-MM-dd hh:mm:ss"),这个时间就是你点鼠标那一刻的时间。
这就解决传时间的bug。

posted on 2006-10-24 18:30 冷面阎罗 阅读(520) 评论(0)  编辑  收藏


只有注册用户登录后才能发表评论。


网站导航: