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

CentOS 5.2 使用 YUM 將 PHP 5.1.6 更新到 5.2.6

CentOS 5.2 使用 ...

CentOS 5. 版內建的 PHP 版本為 5..,甚至於 5. 版仍舊是 5.. 版本。 基於系...

querySelectorAll對多個相同ID做統一處理

querySelectorA...

a 範例,想將同一組ID的DIV做統一隱藏的動作 var elms = document.query...

JavaScript 檢查 Radio Button

JavaScript 檢查 ...

要用 JavaScript 來檢查同一組的 radio button 是否已選,可用以下方法: if...

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