<html>
<head>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/jasmine/3.1.0/jasmine.min.css"/>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jasmine/3.1.0/jasmine.min.js"></script>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jasmine/3.1.0/jasmine-html.min.js"></script>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jasmine/3.1.0/boot.min.js"></script>
</head>
<body></body>
</html>
function getDaysBetweenTwoDates(firstDateInISO, secondDateInISO) {
const startDate = new Date(firstDateInISO);
const endDate = new Date(secondDateInISO);
const firstDate = Date.UTC(
startDate.getFullYear(),
startDate.getMonth(),
startDate.getDate()
);
const secondDate = Date.UTC(
endDate.getFullYear(),
endDate.getMonth(),
endDate.getDate()
);
return Math.abs(
(firstDate.valueOf() - secondDate.valueOf()) / (24 * 60 * 60 * 1000)
);
}
// Assertion below
describe("getDaysBetweenTwoDates", function () {
it("should calculate days between date from the past to the future", () => {
expect(
getDaysBetweenTwoDates(
"2024-01-04T00:00:00+02:00",
"2024-07-04T00:00:00+02:00"
)
).toEqual(183);
});
it("should calculate days between date from the future to the past", () => {
expect(
getDaysBetweenTwoDates(
"2024-07-04T00:00:00+02:00",
"2024-01-04T00:00:00+02:00"
)
).toEqual(183);
});
});
This Pen doesn't use any external JavaScript resources.