Login

Date Picker Template Include

Author:
brandonh
Posted:
November 18, 2008
Language:
HTML/template
Version:
Not specified
Score:
0 (after 0 ratings)

All you need to do is include the file you save this as in your other templates. The css and javascript is right in the file. Fully customizable. Very easy.

Click a date to choose a single date. Click a second date to choose a range. A third click will act as a first click.

note: you also will want to customize the django template specific variables in elements with ids: s_date e_date cal_month

if you change those, it's not even really django specific, but i use this in my form for time off requests at work.

  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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
<style type='text/css'>
    #cal_contain {
        display:block;
        width:100%;
    }
    #cal_dates,#cal_grid,#cal_inst {
        float:left;
        margin-left:4px;
        margin-right:6px;
        /*border:1px solid black;*/
    }
    #cal_grid{
    }
    #cal_dates,#cal_inst{
        margin-top:50px;
        width:25%;
    }
    #cal_inst p {
        margin:0;
    }
    #cal_table{
        text-align:center;
    }
    .c_row td div.clickable {
        cursor:pointer;
        display:block;
        width:100%;
        height:100%;
    }
    .cal_link{
        cursor:pointer;
    }
    .selected{
        background-color:#000;
        color:#eee;
    }
</style>

<script language="javascript">
    function getcurrentdates(){
        var s = document.getElementById('s_date');
        var e = document.getElementById('e_date');
        var s_d = s.value.split("/");
        var e_d = e.value.split("/");
        var d = new Array();
        d['s_mon'] = s_d[0] - 1;
        d['s_day'] = s_d[1];
        d['s_year'] = s_d[2];
        d['e_mon'] = e_d[0] - 1;
        d['e_day'] = e_d[1];
        d['e_year'] = e_d[2];
        return d;
    }
    var init = 1;
    function changedates(month,day,year){
        var d = getcurrentdates();
        var clicked_date = new Date();
        var s_date = new Date();
        var e_date = new Date();
        var s = document.getElementById('s_date');
        var e = document.getElementById('e_date');
        clicked_date.setFullYear(year,month,day);
        s_date.setFullYear(d['s_year'],d['s_mon'],d['s_day']);
        e_date.setFullYear(d['e_year'],d['e_mon'],d['e_day']);
        
        var newmonth = clicked_date.getMonth() + 1;
        var newyear = clicked_date.getFullYear();
        var newday = clicked_date.getDate();
        
        if (init % 2 == 1){
            s.value = newmonth + '/' + newday + '/' + newyear;
            e.value = newmonth + '/' + newday + '/' + newyear;
            createcal(newmonth-1,newyear);
        }
        else{
            //check if dates are the same. if they are, compare to the clicked date. if not, change the start/end date depending on where the click was
            //click before changes start date, click after changes end date. if they are already different, clicked_date is set to both s and e
            if(s_date > clicked_date){
                s.value = newmonth + '/' + newday + '/' + newyear;
                createcal(newmonth-1,newyear);
            }
            else if(e_date < clicked_date){
                e.value = newmonth + '/' + newday + '/' + newyear;
                createcal(newmonth-1,newyear);
            }
            else{//it equals both
                return;
            }
        }
        init++;
    }
    function updatecal(dir,m,y){
        var year = y;
        var newmonth = m;
        switch(dir){
            case 1:
                if(m == 11){
                    newmonth = 0;
                    year++;
                }
                else{
                    newmonth++;
                }
                break;

            default:
                if(m == 0){
                    newmonth = 11;
                    year--;
                }
                else{
                    newmonth--;
                }
                break;
        }
        createcal(newmonth,year);
    }
    function createcal(month,year,today){
        //get current set dates
        var d = getcurrentdates();
        var s_date = new Date();
        var e_date = new Date();
        s_date.setTime(0);
        e_date.setTime(0);
        s_date.setFullYear(d['s_year'],d['s_mon'],d['s_day']);
        e_date.setFullYear(d['e_year'],d['e_mon'],d['e_day']);
        //clear cal
        for(i=1;i<42;i++){
            var box = document.getElementById('c'+i);
            box.innerHTML = '';
            box.onclick = '';
            box.className = '';
        }
        var firstday = getfirst(month,year);
        var numdays = getnumdays(month,year);
        var months = new Array('Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec');
        document.getElementById('cal_month').innerHTML = "<span title='previous month' class='cal_link' onclick='updatecal(-1,"+month+","+year+");'>&lt;</span> " + months[month] + " " + year + " <span title='next month' class='cal_link' onclick='updatecal(1,"+month+","+year+");'>&gt;</span>";
        var f = 0;
        var date_test = new Date();
        date_test.setTime(0);
        for (i=firstday;i<42;i++){
            if(f<=numdays){
                f++;
                box = document.getElementById('c'+i);
                box.innerHTML = "<div class='clickable' onclick='changedates("+month+","+f+","+year+");'>"+f+"</div>";
                /*box.onclick = function() {
                    changedates(month,f,year);
                }*/
                date_test.setFullYear(year,month,f);
                if(e_date >= date_test && date_test >= s_date){
                    box.className = 'selected';
                }
            }
        }
    }
    function getfirst(month,year){
        var firstday = new Date();
        firstday.setFullYear(year);
        firstday.setMonth(month);
        firstday.setDate(1);
        return firstday.getDay() + 1; //returns 1 for sun - 7 for sat (after the plus 1
    }
    function getnumdays(month,year){
        var day = new Date();
        day.setFullYear(year);
        day.setMonth(month+1);
        day.setDate(-1);
        return day.getDate();
    }
    function c(){
        var today = new Date();
        var month = today.getMonth(); //remember months are 0-11
        var year = today.getFullYear();
        var date = today.getDate();
        createcal(month,year,date);
    }
</script>

<div id="cal_contain">
    <div id='cal_dates'>
        From: <input disabled size='10' type='text' name='s_date' id='s_date' value='{% if object %}{{ object.start_date|date:"n/j/Y" }}{% else %}{% now "n/j/Y" %}{% endif %}' /><br />
        To: &nbsp; &nbsp; <input disabled size='10' type='text' name='e_date' id='e_date' value='{% if object %}{{ object.end_date|date:"n/j/Y" }}{% else %}{% now "n/j/Y" %}{% endif %}' />
    </div>
    <div id='cal_grid'>
        <table id='cal_table'>
            <tr id='cal_nav'>
               <td colspan='7' id='cal_month'>&lt; {% now "M Y" %} &gt;</td>
            </tr>
            <tr id='cal_head'>
                <td>Sun</td>
                <td>Mon</td>
                <td>Tue</td>
                <td>Wed</td>
                <td>Thu</td>
                <td>Fri</td>
                <td>Sat</td>
            </tr>
            <tr id='cal_row1' class='c_row'>
                <td id='c1'></td>
                <td id='c2'></td>
                <td id='c3'></td>
                <td id='c4'></td>
                <td id='c5'></td>
                <td id='c6'></td>
                <td id='c7'></td>
            </tr>
            <tr id='cal_row2' class='c_row'>
                <td id='c8'></td>
                <td id='c9'></td>
                <td id='c10'></td>
                <td id='c11'></td>
                <td id='c12'></td>
                <td id='c13'></td>
                <td id='c14'></td>
            </tr>
            <tr id='cal_row3' class='c_row'>
                <td id='c15'></td>
                <td id='c16'></td>
                <td id='c17'></td>
                <td id='c18'></td>
                <td id='c19'></td>
                <td id='c20'></td>
                <td id='c21'></td>
            </tr>
            <tr id='cal_row4' class='c_row'>
                <td id='c22'></td>
                <td id='c23'></td>
                <td id='c24'></td>
                <td id='c25'></td>
                <td id='c26'></td>
                <td id='c27'></td>
                <td id='c28'></td>
            </tr>
            <tr id='cal_row5' class='c_row'>
                <td id='c29'></td>
                <td id='c30'></td>
                <td id='c31'></td>
                <td id='c32'></td>
                <td id='c33'></td>
                <td id='c34'></td>
                <td id='c35'></td>
            </tr>
            <tr id='cal_row6' class='c_row'>
                <td id='c36'></td>
                <td id='c37'></td>
                <td id='c38'></td>
                <td id='c39'></td>
                <td id='c40'></td>
                <td id='c41'></td>
                <td id='c42'></td>
            </tr>
        </table>
    </div>
    <div id='cal_inst'>
        <p>Click a date to choose a single date.
        Click a second date to choose a range.
        A third click will act as a first click.</p>
    </div>
</div>

More like this

  1. Bootstrap Accordian by Netplay4 5 years, 11 months ago
  2. Bootstrap theme for django-endless-pagination? by se210 8 years, 11 months ago
  3. Bootstrap theme for django-endless-pagination? by se210 8 years, 11 months ago
  4. Reusable form template with generic view by roldandvg 9 years ago
  5. Pagination Django with Boostrap by guilegarcia 9 years, 2 months ago

Comments

Please login first before commenting.