var now = new Date(); var timeText = [ now.getHours(), now.getMinutes(), now.getSeconds(), now.getMilliseconds() ].join(':'); console.log(timeText); 結果はこう 17:2:27:87 ですが、本当はこう 17:02:27:0087 したいですよね。この先頭をゼロ0で埋めるってのがゼロフィルです。 やり方はいろいろあるんですが、 .slice()に負数を与えるのが綺麗で良いです。 var now = new Date(); var timeText = [ ("0" + now.getHours()).slice(-2), ("0" + now.getMinutes()).slice(-2), ("0" + now.getSeconds()).s
![めっちゃスマートなゼロフィル(JavaScript おれおれ Advent Calendar 2011 – 1日目) | Ginpen.com](https://cdn-ak-scissors.b.st-hatena.com/image/square/de4af7f62b39439905833504e59ba1e153be6d8b/height=288;version=1;width=512/https%3A%2F%2Fginpen.com%2Fwp-content%2Fthemes%2Fginpen-theme%2Fimg%2Ficon-512.png)