Script Tips


NetBizCity - Script Tips
Tuesday, 28 April 2009 01:42

Time Left Counter

This script will give you a count down of Years, Months, Days and Hours left to a given date.

 

Enter a date as Month day, year

 

Click to see the days left

<script>
function cdtime( targetdate){

var currentTime=new Date()
var tdate=new Date(targetdate)
var timesup=false
var timediff=(tdate - currentTime)/1000
var oneHour=3600 //hour unit in seconds
var oneDay=86400 //day unit in seconds
var oneMonth=2630880 //month unit in seconds
var oneYear=31570560
var yearfield=Math.floor(timediff/oneYear)
timediff = timediff - (yearfield * oneYear)
var monthfield=Math.floor(timediff/oneMonth)
timediff = timediff - (monthfield * oneMonth)
var dayfield=Math.floor(timediff/oneDay)
timediff = timediff - (dayfield * oneDay)
var hourfield=Math.floor(timediff/oneHour)
if (currentTime >= tdate){
	var ret = "The time is up!"
}else{
	if (yearfield > 1){ret = yearfield + " Years " }else{ret = yearfield + " Year "}
	if (monthfield > 1){ret = ret + monthfield + " Months "}else{ret = ret + monthfield + " Month "}
	if (dayfield > 1){ret = ret + dayfield + " days and " }else{ret = ret + dayfield + " day and " }
	if (hourfield > 1){ret = ret + hourfield + " Hours left"}else{ret = ret + hourfield + " Hour left"}
}

return ret
}
</script>

To call it just place this code on your page.



document.write(cdtime("July 1, 2010"))
Published in HTML

 

 

Script Tips Search

Registered User