Back

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

How can I format a JavaScript date object to print as 10-Aug-2010?

1 Answer

0 votes
by (106k points)

Use the below-mentioned code to format a JavaScript date:-

function formatDate(date) { 

var monthNames = [ "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" ]; 

var day = date.getDate(); 

var monthIndex = date.getMonth(); 

var year = date.getFullYear(); 

return day + ' ' + monthNames[monthIndex] + ' ' + year;

console.log(formatDate(new Date()));

image

Related questions

0 votes
1 answer
+1 vote
1 answer
0 votes
1 answer
0 votes
1 answer

Browse Categories

...