Thursday, November 5, 2015

how to format a Date in MM/dd/yyyy HH:mm:ss format in javascript or jQuery?

Type1:
new Date()
Thu Nov 05 2015 16:11:03 GMT+0530 (India Standard Time)


Type 2:
var d = new Date,
    dformat = [d.getMonth()+1,
               d.getDate(),
               d.getFullYear()].join('/')+' '+
              [d.getHours(),
               d.getMinutes(),
               d.getSeconds()].join(':');
alert(dformat)

Type 3:
var d = new Date();
alert(
    ("00" + (d.getMonth() + 1)).slice(-2) + "/" +
    ("00" + d.getDate()).slice(-2) + "/" +
    d.getFullYear() + " " +
    ("00" + d.getHours()).slice(-2) + ":" +
    ("00" + d.getMinutes()).slice(-2) + ":" +
    ("00" + d.getSeconds()).slice(-2)
);

No comments:

Post a Comment