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

ajax防止重複提交

ajax防止重複提交

會發生重提交的原因,大致上是因為考驗人與系統間的耐性~ AJAX(非同步的JavaScript與XM...

Linux底下Apache啟動、停止、重啟指令

Linux底下Apache啟...

a5 如果apache安裝成為linux的服務的話,可以用以下命令操作: service httpd...

整合Facebook粉絲專頁的即時訊息,Zotabox網站打造免費線上支援平台。

整合Facebook粉絲專頁...

還記得在msn的年代,msn有提供在網站上架構即時通訊功能,方便網站管理者與user直接交談~ 現在...

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