JavaScript Date Format dd/mm/yyyy

JavaScript String Date format dd/mm/yyyy hh:m:s  using a 24 hour clock:

<script>

function formatDate() {
var d = new Date();
var dd = d.getDate();
var mm = Number(d.getMonth() + 1);
var yyyy = d.getFullYear();

if (mm.toString().length == 1)
mm = “0” + mm;

var hh = d.getHours();
var m = d.getMinutes();
var s = d.getSeconds();

if (hh.toString().length == 1)
hh = “0” + hh;

return mm + “/” + dd + “/” + yyyy + ” ” + hh + “:” + m + “:” + s;
}

</script>

for meridian timing / 12 hour clocks ……

JavaScript String Time in Merdian format AM/PM

<script>

function MeridianDate() {
var d = new Date();
var hh = d.getHours();
var m = d.getMinutes();
var s = d.getSeconds();
var p = “AM”;
var h = hh;
if (h >= 12) {
h = hh – 12;
p= “PM”;
}
if (h == 0) {
h = 12;
}

return h + ” ” + p;
}

</script>

JavaScript String Date format dd/mm/yyyy hh:m:s am/pm  using a 12 hour clock:

<script>

function formatDate() {
var d = new Date();
var dd = d.getDate();
var mm = Number(d.getMonth() + 1);
var yyyy = d.getFullYear();

if (mm.toString().length == 1)
mm = “0” + mm;

var hh = d.getHours();
var m = d.getMinutes();
var s = d.getSeconds();

var p= “AM”;
var h = hh;
if (h >= 12) {
h = hh – 12;
p= “PM”;
}
if (h == 0) {
h = 12;
}

if (hh.toString().length == 1)
hh = “0” + hh;

return dd+ “/” + mm+ “/” + yyyy + ” ” + hh + “:” + m + “:” + s + ”  ” + p;
}

</script>

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s