Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
4 views
in Java by (13.1k points)

Can anyone help me how I can able to get the date time in JavaScript with this format:

31/12/2010 03:55 AM

Any help would be appreciated.

1 Answer

0 votes
by (26.7k points)

You can use the below code to do it:

//Pad given value to the left with "0"

function AddZero(num) {

    return (num >= 0 && num < 10) ? "0" + num : num + "";

}

window.onload = function() {

    var now = new Date();

    var strDateTime = [[AddZero(now.getDate()), 

        AddZero(now.getMonth() + 1), 

        now.getFullYear()].join("/"), 

        [AddZero(now.getHours()), 

        AddZero(now.getMinutes())].join(":"), 

        now.getHours() >= 12 ? "PM" : "AM"].join(" ");

    document.getElementById("Console").innerHTML = "Now: " + strDateTime;

};

I hope this will help.

Want to become a Java Expert? Join Java Training now!!

Want to know more about Java? Watch this video on Java Tutorial for Beginners | Java Programming:

Related questions

0 votes
1 answer
0 votes
1 answer
0 votes
1 answer
0 votes
1 answer

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...