Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do i find the difference between two dates using Javascript

Tags:

javascript

I want to get reaming days to go particular date so i am trying to detection of particular date with today date. but this not working here is my code If the date is next month 27 how can i get remaining days to go

    var date2=new Date();
    var date1=27/5/2012;
    var diff = date1.getDate()-date2.getDate();
    var date_reaming = diff.getDate();
    document.write(date_reaming + 'days to go');
like image 515
Suresh Pattu Avatar asked Apr 19 '12 09:04

Suresh Pattu


1 Answers

Your code

date1=27/5/2012

Actually means 27 divided by 5 divided by 2012. It is equivalent to writing

date1 = 0.0026838966202783303

date1 will be a number, and this number has no getDate method.

If you declared them as actual date objects instead

var date2 = new Date(2012, 3, 19);
var date1 = new Date(2012, 4, 27);

You would be able to perform

var diff = date1 - date2;

This would give you the difference in milliseconds between the two dates.

From here, you could calculate the number of days like so:

var days = diff / 1000 / 60 / 60 / 24;
like image 59
David Hedlund Avatar answered Sep 19 '22 13:09

David Hedlund