0

How i could convert datetime 5/8/2011 12:00:00 AM (m/d/yyyy) to dd-MMM-yyyy like 08-May-2011 in javascript.

MayureshP
  • 2,514
  • 6
  • 31
  • 41
  • I don't think you'd ever want `5/8/2011` to be `08-Jun-2011`. I guess you meant `08-May-2011`? (although if I saw `5/8/2011` I'd read it as 5th of August. ambiguous date formats are a *really* bad idea) – Spudley Jun 22 '11 at 10:38
  • yeah it's right, but you know the format whatever you get so just extract with javascript function and create new formatted date – Pratik Jun 22 '11 at 10:43

2 Answers2

0

This link is a good resource you can use for.

http://blog.stevenlevithan.com/archives/date-time-format

Alternatively, you need to get the individual part and concatenate them as needed like below.

var now = new Date();
var hour        = now.getHours();
var minute      = now.getMinutes();
var second      = now.getSeconds();
var monthnumber = now.getMonth();
var monthday    = now.getDate();
var year        = now.getYear();

String myOutput = monthday + "-" + monthnumer + "-" + year;

To get the month name instead of month number, you need to define an array like below

var arrMonths = new Array ("Jan","Feb"....};

   String myOutput = monthday + "-" + arrMonths[monthnumer-1] + "-" + year;
AjayR
  • 4,169
  • 4
  • 44
  • 78