0

I want to get the difference between tow dates in javascript but my problem is that the first date is the currenet date of the PC :

today = new Date();

and the other date is a text format date like this for example:

other_date = '15.11.2013';

I want the today date to be same format as other_date and then subtract them , How can I change the format of the today to make the same as other_date ? and How I can make them both as date format to subtract them and get the difference correctly???

Basel
  • 1,305
  • 7
  • 25
  • 34
  • possible duplicate of [Get difference between 2 dates in javascript?](http://stackoverflow.com/questions/3224834/get-difference-between-2-dates-in-javascript) -- please use the search before you ask a new question. – Felix Kling Nov 15 '13 at 08:07
  • also duplicate of [How do I get the number of days between two dates in JavaScript?](http://stackoverflow.com/a/543152/218196). – Felix Kling Nov 15 '13 at 08:39

1 Answers1

1

I would simply use the split function to get the date parts of the date string:

var today = new Date();
var other_date = '15.11.2013';
var dateparts = other_date.split('.');
var otherDate = new Date(dateparts[2], dateparts[1]-1, dateparts[0]); // substract 1 month because month starting with 0

var difference = today.getTime() - otherDate.getTime(); // difference in ms
tschiela
  • 5,231
  • 4
  • 28
  • 35