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

在PHP裡面如何做到基本的SQL injection防禦?

在PHP裡面如何做到基本的S...

資訊安全一直是很頭痛的問題....我一直以來也在尋找最好的方式... 但在尋找的過程中...至少也得...

PDO連接Mysql方式 MVC上應用

PDO連接Mysql方式 M...

pexelsphoto5 將連結資料庫的方式加入model function connect() {...

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