-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdateDifferenceCalculator.js
More file actions
38 lines (32 loc) · 1.36 KB
/
dateDifferenceCalculator.js
File metadata and controls
38 lines (32 loc) · 1.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
// Function to calculate the number of days between two dates
function calculateDateDifference(date1, date2) {
const msPerDay = 24 * 60 * 60 * 1000; // Number of milliseconds per day
const diffInMs = new Date(date2) - new Date(date1);
return Math.round(diffInMs / msPerDay);
}
// Function to format a date in YYYY-MM-DD format
function formatDate(date) {
const d = new Date(date);
const year = d.getFullYear();
const month = String(d.getMonth() + 1).padStart(2, '0');
const day = String(d.getDate()).padStart(2, '0');
return `${year}-${month}-${day}`;
}
// Function to handle user input
function handleUserInput() {
// Get the first date from the user
const startDate = prompt('Enter the start date (YYYY-MM-DD):');
// Get the second date from the user
const endDate = prompt('Enter the end date (YYYY-MM-DD):');
// Validate the input
if (Date.parse(startDate) && Date.parse(endDate)) {
const formattedStartDate = formatDate(startDate);
const formattedEndDate = formatDate(endDate);
const daysDifference = calculateDateDifference(startDate, endDate);
alert(`Start Date: ${formattedStartDate}\nEnd Date: ${formattedEndDate}\nNumber of days between the two dates: ${daysDifference}`);
} else {
alert('Invalid dates. Please enter dates in the format YYYY-MM-DD.');
}
}
// Call the function to handle user input
handleUserInput();