Home » Javascript » javascript – how to get diffrence day between two days in angularJs at form of (yyyy-mm-dd)

javascript – how to get diffrence day between two days in angularJs at form of (yyyy-mm-dd)

Posted by: admin February 23, 2020 Leave a comment

Questions:

I have tow days YYYY-MM-DD like (2020-02-21) one is the current day and the other is a day from my server at a from (dd-mm-yy) like (21-02-2020) then guys I need to get how many days between those 2.

How to&Answers:

Try this as per your Date format.

    let date1 = new Date("2020-02-24");
    let date2 = new Date("2020-02-21");
    let Difference_In_Time = date1.getTime() - date2.getTime();
    let Difference_In_Days: any = Difference_In_Time / (1000 * 3600 * 24);
    console.log(Difference_In_Days);

Answer:

You can calc the difference of two dates in millis and then get the result in days:

var date1 = new Date('2020-02-10');
var date2 = new Date('2020-02-20');
console.log((date2.getTime() - date1.getTime()) / 1000 / 60 / 60 / 24);

The difference is in milliseconds.

To pass from milliseconds to days we can do: /1000 (1000 millis a second) /60 (secs a minute) /60 (60 minutes an hour) / 24 (24 hours a day).

And, by the way, this is done just in javascript.

And to change the date format you can use this:

var date = "10-02-2020";
var newdate = new Date(date.split("-").reverse().join("-"));