-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
49 lines (40 loc) · 2.04 KB
/
script.js
File metadata and controls
49 lines (40 loc) · 2.04 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
39
40
41
42
43
44
45
46
47
48
49
document.addEventListener('DOMContentLoaded', () => {
// Function to set the minimum selectable date to today
function setMinDate() {
const today = new Date().toISOString().split('T')[0];
document.getElementById('Input').setAttribute('min', today);
}
setMinDate(); // Call the function to set the minimum date
function formatDate(date, format) {
const day = date.getDate();
const month = date.getMonth() + 1; // Months are zero-indexed
const year = date.getFullYear();
const shortYear = year.toString().slice(-2); // Last two digits of the year
switch (format) {
case 'writtenFormat':
const months = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"];
return `${day}${day === 1 ? 'st' : day === 2 ? 'nd' : day === 3 ? 'rd' : 'th'} ${months[month - 1]} ${year}`;
case 'dateFirst':
return `${String(day).padStart(2, '0')}/${String(month).padStart(2, '0')}/${year}`;
case 'monthFirst':
return `${String(month).padStart(2, '0')}/${String(day).padStart(2, '0')}/${year}`;
case 'yearFirst':
return `${year}/${String(month).padStart(2, '0')}/${String(day).padStart(2, '0')}`;
case 'notFullYear':
return `${String(day).padStart(2, '0')}/${String(month).padStart(2, '0')}/${shortYear}`;
default:
return '';
}
}
// Handle format change event
document.getElementById('formatSelect').addEventListener('change', () => {
const selectedDate = new Date(document.getElementById('Input').value);
const selectedFormat = document.getElementById('formatSelect').value;
if ( selectedFormat) {
const formatted = formatDate(selectedDate, selectedFormat);
document.getElementById('formattedDate').textContent = formatted;
} else {
document.getElementById('formattedDate').textContent = '';
}
});
});