Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //quick code to create one or more clocks on a site with the ability to adjust each one given a UTC offset (in seconds)
- //sample HTML
- // <span class="clock"><!-- UTC time --></span>
- // <span class="clock" data_offset="-10800"><!-- UTC-3 --></span>
- $(function() {
- init_clocks( );
- });
- var clock_update_timer;
- var weekday = new Array(7);
- weekday[0]= 'Sun';
- weekday[1] = 'Mon';
- weekday[2] = 'Tues';
- weekday[3] = 'Wed';
- weekday[4] = 'Thurs';
- weekday[5] = 'Fri';
- weekday[6] = 'Sat';
- function init_clocks( ){
- if($('.clock').length == 0){ return; }
- update_clocks( );
- //just in case
- clearInterval(clock_update_timer);
- clock_update_timer = setInterval(update_clocks, 1000);
- }
- function update_clocks( ){
- $('.clock').each(function( ){
- offset_seconds = parseInt($(this).data('offset'));
- utc = new Date( );
- currentTime = new Date((new Date(utc.getUTCFullYear( ), utc.getUTCMonth( ), utc.getUTCDate( ), utc.getUTCHours( ), utc.getUTCMinutes( ), utc.getUTCSeconds( ))).getTime( ) + (offset_seconds * 1000));
- currentHours = currentTime.getHours( );
- currentMinutes = currentTime.getMinutes( );
- currentSeconds = currentTime.getSeconds( );
- currentWDay = weekday[currentTime.getDay( )];
- timeOfDay = (currentHours < 12) ? 'am' : 'pm';
- currentHours = (currentHours > 12) ? currentHours - 12 : currentHours;
- currentHours = (currentHours == 0) ? 12 : currentHours;
- currentHours = (currentHours < 10 ? '0' : '') + currentHours;
- currentMinutes = (currentMinutes < 10 ? '0' : '') + currentMinutes;
- currentSeconds = (currentSeconds < 10 ? '0' : '') + currentSeconds;
- // Compose the string for display
- currentTimeString = currentWDay + ' ' + currentHours + ':' + currentMinutes + ':' + currentSeconds + ' ' + timeOfDay;
- $(this).text(currentTimeString);
- });
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement