-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdate-range-picker-bootstrap.html
108 lines (99 loc) · 4.71 KB
/
date-range-picker-bootstrap.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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
<html>
<head>
<title>Date Range Example</title>
<meta http-equiv="Cache-Control" content="no-store" />
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet" crossorigin="anonymous">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js" type="text/javascript"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js" crossorigin="anonymous"></script>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-datepicker/1.7.1/js/bootstrap-datepicker.min.js"></script>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-datepicker/1.7.1/css/bootstrap-datepicker3.css"
/>
</head>
<body>
<div class="container container-table">
<div class="jumbotron jumbotron-fluid">
<div class="container">
<h1 class="display-3">Date Range Example</h1>
<p class="lead">The example demonstatrates the bootstrap date picker and checking for the correct date range betweeen start
date and end date.</p>
<h3>The range is start date + 60 days</h3>
</div>
</div>
<div class="row vertical-center-row">
<div class="text-center">
<form>
<div class="form-inline">
<label for="start-date-one" class="text-center">Start Date</label>
<span>—
<span>
<label for="end-date-one">End Date</label>
</div>
<div class="form-inline">
<input type="text" class="form-control" id="start-date-one" placeholder="MM/DD/YYY" name="start-date"> to
<input type="text" class="form-control" id="end-date-one" placeholder="MM/DD/YYY" name="end-date">
</div>
</form>
<button type="button" id="submitButton" class="btn btn-success" style="margin: 10px;">Check Date Range</button>
<div id="errorMsg"></div>
</div>
</div>
</div>
<script>
$(document).ready(function () {
console.log('HERE');
var date_start_input = $('input[name="start-date"]');
var date_end_input = $('input[name="end-date"]');
var start_options = {
format: 'mm/dd/yyyy',
todayHighlight: true,
autoclose: true,
};
console.log('start_options', start_options);
var end_options = {
format: 'mm/dd/yyyy',
todayHighlight: true,
autoclose: true,
};
date_start_input.datepicker(start_options);
date_end_input.datepicker(end_options);
$('#submitButton').click(function () {
var data = {
startDate: $('#start-date-one').val(),
endDate: $('#end-date-one').val(),
command: $('#command').val(),
};
console.log('data', data.startDate);
function isValidDate(dateStr) {
var d = new Date(dateStr);
return !isNaN(d.getDate());
}
function rangeCheck(data) {
var start = new Date(data.startDate);
var end = new Date(data.endDate);
var targetDate = new Date(data.startDate);
targetDate.setDate(start.getDate() + 60);
console.log('start', start);
console.log('end', end);
console.log('Target--', targetDate);
console.log('Is the date within the range: ', !(end > targetDate))
return !(end > targetDate)
}
if (isValidDate(data.startDate) && isValidDate(data.endDate) && rangeCheck(data)) {
var temp = JSON.stringify(data);
console.log('TEmp', temp);
//this.submit()
const success = `<div class="alert alert-success" role="alert">
<strong>Well done!</strong> Dates are correct within date range.
</div>`;
$('#errorMsg').html(success);
} else {
const fail = `<div class="alert alert-danger" role="alert">
<strong>Oh snap!</strong> Enter valid dates. The date range is 60 days.
</div>`;
$('#errorMsg').html(fail);
}
})
})
</script>
</body>
</html>