在JS中獲取系統(tǒng)當(dāng)前時(shí)間是用到Date這個(gè)對(duì)象,在實(shí)際應(yīng)用時(shí),常常會(huì)用到不同的顯示格式,下面是一些處理方法!
? var now = new Date();
? strTime = now.toLocaleString();
? strYear = now.getYear();
? strMonth = now.getMonth();
? strDay = now.getDay();
?? 如果要將時(shí)間顯示為2007/02/12這種格式,人們往往會(huì)采用如下的方式:
?? displayTime = strYear +"/"+strMonth+"/"+strDay;
?? 也可以加入一些判斷,以滿足要求,但在實(shí)際中這種辦法是做不到的,顯示的格式與我們期望的要求,有很大的差別,為了可以滿足要求我們做如下的處理,
?? 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);
?}
這樣做雖然有些麻煩,但具有很強(qiáng)的靈活性。
FeedBack:
# re: 關(guān)于JS獲取系統(tǒng)當(dāng)前時(shí)間
# re: 關(guān)于JS獲取系統(tǒng)當(dāng)前時(shí)間
2008-02-03 10:09 | 1
你 說(shuō)的不對(duì) strDay = now.getDay(); 是對(duì)應(yīng)日期的星期幾而不是你說(shuō)的2007/02/12 回復(fù) 更多評(píng)論
# re: 關(guān)于JS獲取系統(tǒng)當(dāng)前時(shí)間
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);
}
這樣做是不可能實(shí)現(xiàn)的if(strTime.substring(5,6)<10)這一句就有問(wèn)題,應(yīng)該是
if(strTime.substring(5,6) > 0)
然后還需要同樣的同日進(jìn)行判斷 回復(fù) 更多評(píng)論
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);
}
這樣做是不可能實(shí)現(xiàn)的if(strTime.substring(5,6)<10)這一句就有問(wèn)題,應(yīng)該是
if(strTime.substring(5,6) > 0)
然后還需要同樣的同日進(jìn)行判斷 回復(fù) 更多評(píng)論
# re: 關(guān)于JS獲取系統(tǒng)當(dāng)前時(shí)間
# re: 關(guān)于JS獲取系統(tǒng)當(dāng)前時(shí)間[未登錄](méi)
2008-12-30 13:24 | 北京時(shí)間
你說(shuō)的不對(duì) strDay = now.getDay(); 是對(duì)應(yīng)日期的星期幾而不是你說(shuō)的2007/02/12 回復(fù) 更多評(píng)論
只有注冊(cè)用戶登錄后才能發(fā)表評(píng)論。 | ||
![]() |
||
網(wǎng)站導(dǎo)航:
博客園
IT新聞
Chat2DB
C++博客
博問(wèn)
管理
|
||
| |||||||||
日 | 一 | 二 | 三 | 四 | 五 | 六 | |||
---|---|---|---|---|---|---|---|---|---|
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 | 29 | 30 | 1 | 2 | 3 | 4 | |||
5 | 6 | 7 | 8 | 9 | 10 | 11 |
常用鏈接
留言簿(1)
隨筆檔案
搜索
最新評(píng)論

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