-2

I have a split string lines="Ram Hue, 134, 20.5.1994, 20.4.2004" and I want to get the difference in dates between to dates 20.5.1994 and 20.5.1994, I tried in JavScript but i'ts not working. Also when trying to extract both dates using lines[2] lines[3] I'm getting wrong outputs

var date1 = new Date(lines[2])
var date2 = new Date(lines[3])
var diffDays = parseInt((date2-date1)/(1000*60*60*24),10)
console.log(diffDays)
Shadow Walker
  • 979
  • 5
  • 27
  • 51
  • Does this answer your question? [How do I get the number of days between two dates in JavaScript?](https://stackoverflow.com/questions/542938/how-do-i-get-the-number-of-days-between-two-dates-in-javascript) – MauriceNino May 05 '21 at 07:40
  • @MauriceNino No, My issue was with getting those two dates from the string – Shadow Walker May 05 '21 at 07:56
  • Check this documentation about initializing a Date Object https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date – firatozcevahir May 05 '21 at 07:58

1 Answers1

1

Since lines is a string, lines[2] will just get you the character with index 2 within the string. Instead you need to split the string before:

const arr = lines.split(',');

Then you can access both date strings as arr[2] and arr[3]

lbsn
  • 2,322
  • 1
  • 11
  • 19