-2

Hi i have a variable result of some function with string in a date format (mm-dd-yyyy). I need to convert this string to date in same format like String "23-11-2016" should be converted to Date as 23-11-2016. I dont want any hours or minutes just date like 23-11-206 as i need to pass it to another function. i Have tried all solutions but nothing worked. some lines of code from my script are as follows

date2 = calendar_page.verifyNthDay(today);
//date2 = 23-12-2016
var date_two = new Date(date2);
console.log(date_two);
//Output shows as Invalid Date on console.
Sameen Javid
  • 37
  • 2
  • 9
  • [__"Date Time String Format"__](http://www.ecma-international.org/ecma-262/7.0/index.html#sec-date-time-string-format) – Rayon Nov 25 '16 at 11:49

1 Answers1

0

Try

var date2 = calendar_page.verifyNthDay(today);
    date2 = date2.split('-').reverse().join('-').replace(/-/g,'/');

var date_two = new Date(date2);

console.log(date_two);
user2085143
  • 4,162
  • 7
  • 39
  • 68
  • It doesn't make sense to split the string into it's parts just to create another string that is given to the Date constructor to parse. Once you have the parts, just give them to the constructor: `var b = date2.split('-'); var date_two = new Date(b[2],--b[1],b[0]);` which is even less to type. ;-) – RobG Nov 25 '16 at 14:52
  • Will keep that in mind for next time! Cheers – user2085143 Nov 25 '16 at 14:59