今天作程序的時候用到了,在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。