Sunday, 24 August 2014

Date format using Javascript


Date format mm / dd / yyyy. We can chage format also.




window.onload = function currentDate()
{
var today = new Date();
    var dd = today.getDate();
    var mm = today.getMonth()+1; //January is 0!

    var yyyy = today.getFullYear();
    if(dd<10 span="">
        dd='0'+dd
    }
    if(mm<10 span="">
        mm='0'+mm
    }
    var today = mm+'/'+dd+'/'+yyyy;
$("#datetoday").html(today);
}

Digital Clock

This is a pretty cool way to display the time on your web page using jQuery to get the current time and then refresh the clock every 1 second.




function Clock ( )     {  

  var currentTime = new Date ( ); 
   var currentHours = currentTime.getHours ( ); 
   var currentMinutes = currentTime.getMinutes ();   


 var currentSeconds = currentTime.getSeconds ( );  

 currentMinutes = ( currentMinutes < 10 ? "0" : "" ) + currentMinutes;    

currentSeconds = ( currentSeconds < 10 ? "0" : "" ) + currentSeconds;     
  
   var timeOfDay = ( currentHours < 12 ) ? "AM" : "PM";   
   currentHours = ( currentHours > 12 ) ? currentHours - 12 : currentHours;     

  currentHours = ( currentHours == 0 ) ? 12 : currentHours;  
 
  var currentTimeString = currentHours + ":" + currentMinutes + ":" + currentSeconds + " " + timeOfDay;     
           $("#clock").html(currentTimeString);
           }


  $(document).ready(function() {
    setInterval('Clock()', 1000);
});