How to get current time and date using JavaScript.

Getting system date and time using JavaScript is easy. But some steps has to follow to convert it to a readable format. These functions will help you to get rid of the hassle .

//returns date in format 01/01/2012

function getCurrentDate() {
var currentTime = new Date()
var month = currentTime.getMonth() + 1
var day = currentTime.getDate()
var year = currentTime.getFullYear()
return (month + "/" + day + "/" + year);
}

//returns time in format 05:45 AM
function getCurrentTime() {
var time = ""; var currentTime = new Date(); var hours = currentTime.getHours(); var minutes = currentTime.getMinutes(); if (minutes < 10) { minutes = "0" + minutes; } var h; if (hours > 12) { h = hours - 12; } if (h < 10) { h = '0' + h; } time += (h + ":" + minutes + " "); if (hours > 11) { time += ("PM"); } else { time += ("AM"); } return time;
}

Comments

Popular posts from this blog

Fixing Error "Failed to load VMMR0.r0 (VERR_SUPLIB_WORLD_WRITABLE)" in Virtualbox

Create new Java Class files at runtime

Using iReport plugin for Jasper Reports in Netbeans