在JS中獲取系統當前時間是用到Date這個對象,在實際應用時,常常會用到不同的顯示格式,下面是一些處理方法!
? var now = new Date();
? strTime = now.toLocaleString();
? strYear = now.getYear();
? strMonth = now.getMonth();
? strDay = now.getDay();
?? 如果要將時間顯示為2007/02/12這種格式,人們往往會采用如下的方式:
?? displayTime = strYear +"/"+strMonth+"/"+strDay;
?? 也可以加入一些判斷,以滿足要求,但在實際中這種辦法是做不到的,顯示的格式與我們期望的要求,有很大的差別,為了可以滿足要求我們做如下的處理,
?? if(strTime.substring(5,6)<10){
?var displayTime = strTime.substring(0,4)+"/0"+strTime.substring(5,6)+"/"+strTime.substring(7,9);
?}else{
?var displayTime = strTime.substring(0,4)+"/"+strTime.substring(5,7)+"/"+strTime.substring(8,10);
?}
這樣做雖然有些麻煩,但具有很強的靈活性。
posted @
2007-02-12 14:21 Andy zhang 閱讀(9233) |
評論 (6) |
編輯 收藏