How to format a JavaScript date

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

Text Copied From stackoverflow.com
on : Mon, Aug 23, 2010 Views : 1981005

Accepted Answer



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()));
  
// show current date-time in console

You can edit the array monthNames to use Jan, Feb, Mar, etc..

Text Copied From stackoverflow.com
on : Mon, Aug 23, 2010

Ask Question

If you want to ask question you must LogIn or SignUp
Login SignUp

Quick Links

E-Magazines

@

Total Followers
Study Group Created
Study Group Joined
Following Teacher
Following Organization
Blog Articles Added
Questions Asked
Questions Answered
Jobs Posted
Total Members in Group
Questions asked by members
Tasks added in this Group

Please wait..

Ok

Login to Open ESchool OR Create your account    Login   SignUp