JQuery / javascript datetime

I want to convert this string 23/08/2009 12:05:00 to javascript datetime

How can i do this?

+2


source to share


3 answers


I think this might help you http://www.mattkruse.com/javascript/date/



Theres a function getDateFromFormat()

you can change a little to solve your problem.

+3


source


You can get the parts of a date using a regex and call the Date constructor by customizing the month number since monthly numbers are zero-based, for example:

function customDateParse (input) {
  var m = input.match(/(\d+)/g);
  return new Date(m[2], m[1] - 1, m[0], m[3], m[4], m[5]);
}

customDateParse('23/08/2009 12:05:00');
// Sun Aug 23 2009 12:05:00

      

If you don't like regular expressions:



function customDateParse (input) {
  input = input.split(' ');
  var date = input[0].split('/'),
      time = input[1].split(':');

  return new Date(date[2], date[1] - 1, date[0], time[0], time[1], time[2]);
}

customDateParse('23/08/2009 12:05:00');
// Sun Aug 23 2009 12:05:00

      

But if you find this complex and want to do more Date manipulations, I highly recommend the DateJS library , which is small, open source and syntactic sugar ...

+3


source


Use momentjs to convert from string to Date object.

var date = '23/08/2009 12:05:00';
var datetime = moment(date, 'DD/MM/YYYY HH:mm:ss');
datetime = moment.toDate();

      

+1


source







All Articles