result i want
"6/19/2012"
If that's really what you want, then it's a string, so it's just:
var result = "6/19/2012 5:16:26 PM".split(" ")[0];
Live Example:
document.body.innerHTML = "6/19/2012 5:16:26 PM".split(" ")[0];
But if you want an actual Date
instance, instead of using the one-argument version of Date
, use the multi-argument version (accepts year, month, day, ...):
var myDate="6/19/2012 5:16:26 PM";
myDate=myDate.split(" ")[0].split("/");
alert(new Date(parseInt(myDate[2], 10), parseInt(myDate[0], 10) - 1, parseInt(myDate[1], 10)));
Live Example:
var myDate = "6/19/2012 5:16:26 PM";
myDate = myDate.split(" ")[0].split("/");
document.body.innerHTML = new Date(parseInt(myDate[2], 10), parseInt(myDate[0], 10) - 1, parseInt(myDate[1], 10));
The parseInt(..., 10)
converts from string to number using base 10 (decimal), and that builds a date from just the year, month (they start at 0), and day.