0


I want to get the difference of of these dates. e.g

var current_date = new Date();
var date_from_database = "2013/06/10 15:00:00";
var difference = data_from_datebase - current_date;

   // so the result should be: 7

I want to get how many days left buy subtracting the current day on specific day like the example above. How can I do this on javascript?

Thanks in advance!

Alberto De Caro
  • 5,147
  • 9
  • 47
  • 73
Lian
  • 1,597
  • 3
  • 17
  • 30

4 Answers4

2

The following might work:

var oneDay = 24*60*60*1000; // hours*minutes*seconds*milliseconds
var firstDate = new Date(2008,01,12);
var secondDate = new Date(2008,01,22);

var diffDays = Math.round(Math.abs((firstDate.getTime() - secondDate.getTime())/(oneDay)));
Pavan Kumar K
  • 1,360
  • 9
  • 11
0
var date_from_database = new Date("2013/06/10 15:00:00");
var date1= new Date();
var date_diff = Math.abs(date2.getTime() - date_from_database .getTime()/86400000);
PSR
  • 39,804
  • 41
  • 111
  • 151
0

In order to compare it with the current day:

var firstDate = new Date(2008,01,12);
var secondDate = new Date();

var timeDiff = Math.abs(firstDate.getTime() - secondDate.getTime());
var diffDays = Math.ceil(timeDiff / (1000 * 3600 * 24)); 

alert(diffDays);

http://jsfiddle.net/imac/TEjqX/

You will have to modify the date you receive from the database to addapt it to the needed parameter for date(). You can take a look at this answer for more info.

Community
  • 1
  • 1
Alvaro
  • 40,778
  • 30
  • 164
  • 336
0

Javascript could be very pesky in processing the datetime variables.

Do not reinvent the wheel. I often use the datejs library.

In particular, the time.js file features the TimeSpan() function that processes datetime differences.

References:

Alberto De Caro
  • 5,147
  • 9
  • 47
  • 73