在JS中獲取系統(tǒng)當前時間是用到Date這個對象,在實際應(yīng)用時,常常會用到不同的顯示格式,下面是一些處理方法!
? 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);
?}
這樣做雖然有些麻煩,但具有很強的靈活性。
FeedBack:
# re: 關(guān)于JS獲取系統(tǒng)當前時間
# re: 關(guān)于JS獲取系統(tǒng)當前時間
2008-07-30 16:27 | rzzfch520
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);
}
這樣做是不可能實現(xiàn)的if(strTime.substring(5,6)<10)這一句就有問題,應(yīng)該是
if(strTime.substring(5,6) > 0)
然后還需要同樣的同日進行判斷 回復(fù) 更多評論
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);
}
這樣做是不可能實現(xiàn)的if(strTime.substring(5,6)<10)這一句就有問題,應(yīng)該是
if(strTime.substring(5,6) > 0)
然后還需要同樣的同日進行判斷 回復(fù) 更多評論
只有注冊用戶登錄后才能發(fā)表評論。 | ||
![]() |
||
網(wǎng)站導(dǎo)航:
博客園
IT新聞
Chat2DB
C++博客
博問
管理
|
||
| |||||||||
日 | 一 | 二 | 三 | 四 | 五 | 六 | |||
---|---|---|---|---|---|---|---|---|---|
28 | 29 | 30 | 31 | 1 | 2 | 3 | |||
4 | 5 | 6 | 7 | 8 | 9 | 10 | |||
11 | 12 | 13 | 14 | 15 | 16 | 17 | |||
18 | 19 | 20 | 21 | 22 | 23 | 24 | |||
25 | 26 | 27 | 28 | 1 | 2 | 3 | |||
4 | 5 | 6 | 7 | 8 | 9 | 10 |
常用鏈接
留言簿(1)
隨筆檔案
搜索
最新評論

- 1.?re: 關(guān)于JS獲取系統(tǒng)當前時間
- 你說的不對 strDay = now.getDay(); 是對應(yīng)日期的星期幾而不是你說的2007/02/12
- --北京時間
- 2.?re: 關(guān)于JS獲取系統(tǒng)當前時間[未登錄]
-
@1
就是 - --dd
- 3.?re: 關(guān)于JS獲取系統(tǒng)當前時間
- 恩 這段代碼有問題
- --zf
- 4.?re: 關(guān)于JS獲取系統(tǒng)當前時間
- 評論內(nèi)容較長,點擊標題查看
- --rzzfch520
- 5.?re: 關(guān)于JS獲取系統(tǒng)當前時間
- 你 說的不對 strDay = now.getDay(); 是對應(yīng)日期的星期幾而不是你說的2007/02/12
- --1