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

HTML5 Geolocation

HTML5 Geolocat...

說真的,HTML5 Geolocation的功能真的好用... 輕鬆就可以讓網站也能取得目前所在的地...

Google map 功能導入

Google map 功能導...

blogmap 寫故事還是得有地圖才好厚! 前幾天,sam同我說有一個客戶有Google map的需...

動態留言功能調整

動態留言功能調整

guestmap55 最近改進了朋友動態的發文方式.... 一開始在功能方面加上了googlemap...

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