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.
Asked
Active
Viewed 3,989 times
0

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 Answers
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
-
I have dateTime in var using getElementById,how i convert it to datetime – MayureshP Jun 22 '11 at 11:34
-
1you should write like this, var now =new Date(document.getElementById ("yourid").value); – AjayR Jun 23 '11 at 02:03
0
check below link hope you got some idea
http://bytes.com/topic/javascript/answers/519332-how-convert-datetime-format-using-javascript