-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcalendar-momentjs.html
90 lines (76 loc) · 2.66 KB
/
calendar-momentjs.html
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Moment.js with jQuery and Input Type Date</title>
<!-- jQuery -->
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<!-- Moment.js -->
<script src="https://cdn.jsdelivr.net/npm/[email protected]/moment.min.js"></script>
<style>
input {
position: relative;
width: 150px;
height: 20px;
color: white;
}
input:before {
position: absolute;
top: 3px;
left: 3px;
content: attr(data-date);
display: inline-block;
color: black;
}
input::-webkit-datetime-edit,
input::-webkit-inner-spin-button,
input::-webkit-clear-button {
display: none;
}
input::-webkit-calendar-picker-indicator {
position: absolute;
top: 3px;
right: 0;
color: black;
opacity: 1;
}
</style>
</head>
<body>
<h1>Select a Date</h1>
<form>
<label for="eventDate">Event Date:</label>
<input type="date" class="form-control" format="yyyy-mm-dd" name="afterDate" id="eventDate"
placeholder="dd/mm/yyyy">
<input type="date" class="form-control" name="afterDate" id="eventDate" placeholder="dd/mm/yyyy">
<input type="date" data-date="" data-date-format="DD MM YYYY" value="2015-08-09" />
</form>
<p id="formattedDate"></p>
<script>
$(document).ready(function () {
$("input").on("change", function () {
this.setAttribute(
"data-date",
moment(this.value, "YYYY-MM-DD")
.format(this.getAttribute("data-date-format"))
)
}).trigger("change")
$('#eventDate').on('change', function () {
var selectedDate = $(this).val();
if (selectedDate) {
var formattedDate = moment(selectedDate).format('DD-MM-YYYY');
$('#formattedDate').text('Selected Date: ' + formattedDate);
$(this).val(reformattedDate);
}
});
// Handle focus event (optional, if you want to set placeholder value or perform any logic)
$('#eventDate').on('focus', function () {
if (this.value === "") {
this.value = 'YYYY-MM-DD'; // Optional: set default value when the field is focused
}
});
});
</script>
</body>
</html>