Skip to content

Instantly share code, notes, and snippets.

@rskull
Created October 29, 2011 17:02
Show Gist options
  • Save rskull/1324776 to your computer and use it in GitHub Desktop.
Save rskull/1324776 to your computer and use it in GitHub Desktop.
画像で時計を表示
//日付取得
var now = new Date();
var y = now.getFullYear(); // 年
var mo = now.getMonth() + 1; // 月
var d = now.getDate(); // 日
var h = now.getHours(); // 時
var m = now.getMinutes(); // 分
var s = now.getSeconds(); // 秒
//imgオブジェクト作成
onload = function () {
for (i=0;i<4;i++) {
var t = document.createElement('img');
t.src = 'image/' + String(y).split('')[i] + '.jpg';
$('timer').appendChild(t);
}
for (i=0;i<10;i++) {
var t = document.createElement('img');
if (i%2 == 0) {
var dot = document.createElement('img');
dot.src = 'image/d.jpg';
$('timer').appendChild(dot);
}
t.src = 'image/0.jpg';
t.id = 't' + i;
$('timer').appendChild(t);
}
}
//DOM
function $(id) {
return document.getElementById(id);
}
//ゼロ埋め分割
function dt(d) {
var dt = ("0" + d).slice(-2);
return String(dt).split('');
}
//今月の最終日 + 1 取得
function lastday () {
var y = now.getFullYear();
var m = now.getMonth();
for (i=27;i<=32;i++) {
var check = new Date(y, m, i);
if (check.getMonth() != m) {
return i;
}
}
}
//タイマー起動
setInterval( function () {
var dat = new Array(mo, d, h, m, s);
var k = 0;
s++;
if (s == 60) { s = 0; m++; }
if (m == 60) { m = 0; h++; }
if (h == 24) { h = 0; d++;
if (d == lastday()) { d = 1; mo++;
if (mo == 13) { mo = 1; }
}
}
for (i=0;i<5;i++) {
for (j=0;j<2;j++) {
$('t'+k).src = 'image/' + dt(dat[i])[j] + '.jpg';
k++;
}
}
},1000);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment