Menampilkan Tanggal Saat Ini dengan JavaScript
Letakkan fungsi ini di atas </head>
:
<script>
function dispDate(dateVal) {
DaystoAdd = dateVal;
TodaysDate = new Date();
TodaysDay = new Array('Minggu', 'Senin', 'Selasa', 'Rabu', 'Kamis', 'Jum'at', 'Sabtu');
TodaysMonth = new Array('Januari', 'Februari', 'Maret', 'April', 'Mei', 'Juni', 'Juli', 'Agustus', 'September', 'Oktober', 'November', 'Desember');
DaysinMonth = new Array('31', '28', '31', '30', '31', '30', '31', '31', '30', '31', '30', '31');
function LeapYearTest(Year) {
if (((Year % 400) === 0) || (((Year % 100) !== 0) && (Year % 4) === 0)) {
return true;
} else {
return false;
}
}
CurrentYear = TodaysDate.getYear();
if (CurrentYear < 2000) CurrentYear = CurrentYear + 1900;
currentMonth = TodaysDate.getMonth();
DayOffset = TodaysDate.getDay();
currentDay = TodaysDate.getDate();
month = TodaysMonth[currentMonth];
if (month == 'February') {
if (((CurrentYear % 4) === 0) && ((CurrentYear % 100) !== 0) || ((CurrentYear % 400) === 0)) {
DaysinMonth[1] = 29;
} else {
DaysinMonth[1] = 28;
}
}
days = DaysinMonth[currentMonth];
currentDay += DaystoAdd;
if (currentDay > days) {
if (currentMonth == 11) {
currentMonth = 0;
month = TodaysMonth[currentMonth];
CurrentYear = CurrentYear + 1;
} else {
month = TodaysMonth[currentMonth + 1];
}
currentDay = currentDay - days;
}
DayOffset += DaystoAdd;
function offsettheDate(offsetCurrentDay) {
if (offsetCurrentDay > 6) {
offsetCurrentDay -= 6;
DayOffset = TodaysDay[offsetCurrentDay - 1];
offsettheDate(offsetCurrentDay - 1);
} else {
DayOffset = TodaysDay[offsetCurrentDay];
return true;
}
}
offsettheDate(DayOffset);
TheDate = DayOffset + ', ';
TheDate += currentDay + ' ';
TheDate += month + ' ';
if (CurrentYear < 100) CurrentYear = "19" + CurrentYear;
TheDate += CurrentYear;
document.write(' ' + TheDate);
}
</script>
Setelah itu panggil fungsi dispDate(0)
pada tempat yang Anda inginkan:
<script>
dispDate(0);
</script>
Sumber: Instant Web Site Tool
Versi Saya
Memungkinkan untuk menambahkan waktu jam, detik dan menit serta efek animasi perubahan waktu setiap detik jika dimasukkan ke dalam setInterval()
:
<div id="date-container"></div>
<script>
function showDateTo(elemID) {
var date = new Date();
var second = date.getSeconds();
var minute = date.getMinutes();
var hour = date.getHours();
var day = date.getDay();
var dayMonth = date.getDate();
var month = date.getMonth();
var year = date.getYear();
var dayArray = ["Minggu", "Senin", "Selasa", "Rabu", "Kamis", "Jum'at", "Sabtu"];
var monthArray = ["Januari", "Februari", "Maret", "April", "Mei", "Juni", "Juli", "Agustus", "September", "Oktober", "November", "Desember"];
// Menambahkan angka nol di depan jika nilai kurang dari 10
if (second < 10) second = '0' + second;
if (minute < 10) minute = '0' + minute;
if (hour < 10) hour = '0' + hour;
if (dayMonth < 10) dayMonth = '0' + dayMonth;
if (year < 1000) year += 1900;
document.getElementById(elemID).innerHTML = dayArray[day] + ', ' + dayMonth + ' ' + monthArray[month] + ' ' + year + ' ' + hour + ':' + minute + ':' + second;
}
// Masukkan ke kontainer!
// Pakai interval 1 detik sekali untuk efek animasi jam
// (tanpa interval masih tetap bisa bekerja, tapi tidak akan ada efek animasi)
setInterval(function() {
showDateTo('date-container');
}, 1000);
</script>
Labels: JavaScript, Menengah, Potongan
4 Comments:
??........
mas cara buat tanggal posted yang kyk gni g5n mas???
http://4.bp.blogspot.com/-2fyDMSHKhNs/UOrf7F2r4WI/AAAAAAAAABw/2CVr8qPQBxE/s1600/ada.jpg
By alansinggih, at Monday, January 7, 2013 at 9:48:00 PM GMT+7
tolong bntu yaa....
By alansinggih, at Monday, January 7, 2013 at 9:50:00 PM GMT+7
Taruh ini di atas <div class='post-body'>:
<b:if cond='data:blog.pageType == "item"'>
<p style='text-align:right;font-weight:bold;'>
<b:if cond='data:post.dateHeader'>
<data:post.dateHeader/>
</b:if>
</p>
</b:if>
By Taufik Nurrohman, at Tuesday, January 8, 2013 at 9:38:00 AM GMT+7
nice info,Terus update gan :D
By Bima A., at Friday, April 12, 2013 at 11:36:00 PM GMT+7
Post a Comment
<< Home