0 votes
1 view
in Web Technology by (6.8k points)

How to display the current date and time in JavaScript?

1 Answer

0 votes
by (13k 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
0 votes
1 answer
0 votes
1 answer
Welcome to Intellipaat Community. Get your technical queries answered by top developers !


Categories

...