0

i want to ask about convert datetimepicker to datepicker, i have value datetime, and i want to convert date ? how to convert that? this is my value

2013-11-29 10:12 to 29-11-2013

Thanks.

  • possible duplicate of http://stackoverflow.com/questions/2698725/comparing-date-part-only-without-comparing-time-in-javascript – Just code Nov 29 '13 at 04:05

4 Answers4

0
var date = new Date("2013-11-29 10:12");  //parse the date
var mon = date.getMonth() + 1;
console.log(date.getDate() + '-' + mon  + '-' + date.getFullYear()); //returns 29-11-2013 

Since @RobG mentioned about browser compatibility, here is another approach

var date = "2013-11-29 10:12";
var d = date.substring(0, 10).split("-");
console.log(d[2] + "-" + d[1] +"-" + d[0]); //returns 29-11-2013 
Praveen
  • 55,303
  • 33
  • 133
  • 164
0

If the value is always provided as a string, you can adjust the format using:

// Reformat 2013-11-29 10:12 to 29-11-2013
function formatDateString(s) {
  var s = s.split(/\D/);
  return s[2] + '-' + s[1] + '-' + s[0];
}

Note that this format may be misinterpreted by those who expect dates in month/day/year format.

RobG
  • 142,382
  • 31
  • 172
  • 209
0

If you just want the string, just get the day/month/year substrings you need with substr or substring, if you want the a javascript object, you can use Date.

pgpb.padilla
  • 2,318
  • 2
  • 20
  • 44
0

You could use the javascript lib momentjs to help you handle the time related work.
moment("2013-11-29 10:12", "YYYY-MM-DD HH:mm").format("DD-MM-YYYY");

Lifecube
  • 1,198
  • 8
  • 11