How do i find the difference between two dates using Javascript

前端 未结 6 1675
孤城傲影
孤城傲影 2020-12-31 19:21

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 m

6条回答
  •  独厮守ぢ
    2020-12-31 19:33

    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;
    

提交回复
热议问题