Loading...

JavaScript的獲取日期代碼不足兩位數補0

Temperature: 0 °C

ChungChung
author_tools

們用javascript得到了一個時間後...針對月份及日期的部分只會顯示數字...


function getDateWeek()
{
var now=new Date();
var year=now.getFullYear();//得到4位數年yyyy
var month=now.getMonth()+1;
var date=now.getDate();
var weeknum=now.getDay();

var s=year+"-"+month+"-"+date;
document.write(s);
}

但若們需要把1變成01.....那麼可以使用以下方式....

function Append_zero(obj)
{
if(obj<10) return "0" +""+ obj;
else return obj;

}

function getDateWeek()
{
var now=new Date();
var year=now.getFullYear();//得到4位數年yyyy
var month=now.getMonth()+1;
var date=now.getDate();
var weeknum=now.getDay();

var s=year+"-"+Append_zero(month)+"-"+Append_zero(date)+" ";
document.write(s);
}

https://innstory.com/story-JavaScript的獲取日期代碼不足兩位數補0-282
寫程式筆記

Prev
 2014年聖誕節快樂

Next
2014年的最後一天 

About the Author

Chung

我是chung
網路工作者
主業是網站系統開發建置
副業是做夢,寫故事
作品請參考/teme.biz
做夢請參考/innstory.com
聯絡/chung.teme@gmail.com

#有人用筆寫日記,有人用歲月寫日記,有人用照片寫日記,而我,用innstory寫日記。

Visitor message

Leave some footprints to prove that you visited me

Recommended reading

Author's other related stories

定義404頁面

定義404頁面

什麼是頁面? HTTP 或Not Found錯誤訊息是HTTP的其中一種「標準回應訊息」(HTTP狀...

修練中

修練中

假日二胡修練中。

IE10 在 fckeditor無正常顯示及無法輸入任何字元

IE10 在 fckedit...

於fckorcodeie.js 檔案中, 在B.open(‘GET’, A, false); 及 B...

Please select an option

error

Hi, thank you for your participation, but you cannot vote repeatedly~

Join innstory now and start recording your story.

"Innstory" is a place to store stories. We are committed to becoming a warm platform. Deepening the bonds between people is our direction.
We are convinced that the blockchain between people is not just a cold calculation. Join us now.

Wrong format