Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Web Technology by (7k points)

How to display the current date and time in JavaScript?

1 Answer

0 votes
by (13.1k points)

You can try this;

var date = new Date(),

    minutes = date.getMinutes().toString().length == 1 ? '0'+date.getMinutes() : date.getMinutes(),

    hours = date.getHours().toString().length == 1 ? '0'+date.getHours() : date.getHours(),

    ampm = date.getHours() >= 12 ? 'pm' : 'am',

    months = ['Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec'],

    days = ['Sun','Mon','Tue','Wed','Thu','Fri','Sat'];

return days[date.getDay()]+' '+months[date.getMonth()]+' '+date.getDate()+' '+date.getFullYear()+' '+hours+':'+minutes+ampm;

Want to learn Full Stack Development? Check out the Full Stack Developer course from Intellipaat.

Related questions

0 votes
1 answer
0 votes
1 answer
asked Jan 28, 2021 in Java by dante07 (13.1k points)
–1 vote
1 answer
0 votes
1 answer
asked Feb 16, 2021 in Web Technology by Jake (7k points)

Browse Categories

...