I have a string with a specific format which I am trying to convert to a Date object. I was hoping var dateObject = new Date(myString)
would be sufficient but it won't work. So I tried breaking up the string into smaller pieces, and that didn't work either.
var str = '2015-02-20 11:00';
var obj = new Date(str.substr(2, 4), str.substr(5, 7), str.substr(8, 10), str.substr(13, 15), str.substr(16, 18));
// Above code yields invalid date
How can I make a date object out of my string?
Did you try just
var str = '2015-02-20 11:00';
var obj = new Date(str);
works for me -> http://jsfiddle.net/j5kaC/1/
If for some strange reason that doesn't work, split it up and pass it to the Date constructor in the appropriate places
var str = '2015-02-20 11:00';
var arr = str.split(' ');
var time = arr.pop().split(':')
var arr2 = arr.shift().split('-');
var date = new Date(arr2[0], arr2[1]-1, arr2[2], time.shift(), time.pop(), 0, 0);
console.log(date);
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments