simple-clock.js 1.29 KB
(function($)
{
	$.fn.simpleClock = function()
	{
		function getTime()
		{
			var date = new Date(),
			hour = date.getHours();
			
			return {
				day: $L.days[calculateDay(date.getDay())],
				date: date.getDate(),
				month: $L.months[date.getMonth()],
				hour: appendZero(hour),
				minute: appendZero(date.getMinutes()),
				second: appendZero(date.getSeconds())
			};
		}
		
		// tage umrechen, da in der Languages der wochentag mit Montag beginnt und nicht mit Sonntag wie in JS benötigt
		function calculateDay(weekday)
		{
			if(weekday == 0){
				return 6;
			}
			return weekday - 1;
		}
		
		// appendZero - If the number is less than 10, add a leading zero. 
		function appendZero(num)
		{
			if (num < 10) {
				return "0" + num;
			}
			return num;
		}

		// refreshTime - Build the clock.
		function refreshTime()
		{
			var now = getTime();
			//$('#date-container').html(now.day + ' ' + now.date + '. ' + now.month);
			$('#date-container-day').html(now.day);
			$('#date-container-date').html(now.date);
			$('#date-container-month').html(now.month);
			
			$('#time-container').html("<span class='time-hour'>" + now.hour + "</span>" + ":<span class='time-minute'>" + now.minute + "</span>");

		}

		// Tick tock - Run the clock.
		refreshTime();
		setInterval(refreshTime, 1000);

	};
})(jQuery);