How we can create a date-picker using JavaScript or any JS library? Solution: See this JavaScript Datepicker With jQuery and CSS UI, Form Input Datepicker JS.
Previously I have shared a jQuery UI datepicker, but this program is complete is based on JS (jQuery) without any special library. Basically, datepicker is a type of hidden calendar, which appears on action or click event. When we put our DOB in any form, after clicking on the DOB input field a calendar becomes appear for the select date, month, and year, which is called a datepicker.
Today you will learn to create a form input date picker with JS. Basically, there is a form with 3 fields name, email, and birth date, the first two inputs are basic text inputs. But the third select birth date input has a calendar for choosing your date of birth or any event. When you click on the birth date input, then a calendar will appear with slightly down animation. The calendar has navigation buttons for changing months and years, you can choose your desired date.
So, Today I am sharing JavaScript Datepicker With jQuery and CSS UI design. This program is completely based on JS codes which are powered by jQuery, If we use pure JavaScript then code will little complicated and take more time. This is a complete datepicker program that can be used anywhere as you want, and it has a material type design using CSS.
If you are thinking now how this date picker actually is, then see the preview given below.
Preview Of Date-Picker JS for Forms
See this video preview to getting an idea of how this date picker looks like.
Now you can see this visually, also you can see it live by pressing the button given above. If you like this program, then get the source code of its.
You May Also Like:
JavaScript Datepicker With jQuery and CSS UI Source Code
Before sharing source code, let’s talk about it. First I have created a form and put a heading, a text, 3 text inputs, some divs, and a submit button inside it. There I have used simple HTML form tag and <input type="text"> for creating the fields. For placing icons I have used font-awesome library and put pre-built CSS codes inside divs.
Now using CSS I have placed all the elements in the right place, as you can see in the preview. With CSS I have done basic styling like margin, padding, position, size, color, etc. There I have used some google fonts for text and headings. For creating the little animation effect I have used CSS transition command.
The JavaScript part has the main feature of the program. I have created a whole calendar dynamically using JS which is based on the jQuery library. I have stored months in a variable and did many calculations. The whole calendar structure is created using the JavaScript DOM program. Later the calendar styled using CSS.
Left all other things you will understand after getting the codes, I can’t explain all in writing. For creating this program you have to create 3 files. First file for HTML, second for CSS, and the third file for JavaScript. Follow the steps to creating this program without any error.
index.html
Create an HTML file named ‘style.css‘ and put these codes given below.
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 |
<!DOCTYPE html> <!-- Code By Webdevtrick ( https://webdevtrick.com ) --> <html lang="en" > <head> <meta charset="UTF-8"> <title>JavaScript Date Picker | Webdevtrick.com</title> <script src="https://cdnjs.cloudflare.com/ajax/libs/modernizr/2.8.3/modernizr.min.js"></script> <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/normalize/5.0.0/normalize.min.css"> <link href="https://stackpath.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet"> <link rel="stylesheet" href="style.css"> </head> <body> <form> <h1>Date picker</h1> <p>Click on the birth date field to see the toggle and datepicker.</p> <div class='fields'> <i class='fa fa-user'></i> <input type='text' name='name' placeholder='Name' /> </div> <div class='fields'> <i class='fa fa-envelope-o'></i> <input type='text' name='email' placeholder='Email ID' /> </div> <div class='fields dateSelector'> <i class='fa fa-calendar-o'></i> <input type='text' name='birthday' id='DOB' placeholder='Birth date' /> </div> <input type="submit" class='button' value='Submit'> </form> <script src='https://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.min.js'></script> <script src="function.js"></script> </body> </html> |
style.css
Now create a CSS file named ‘style.css‘ and put these codes given here.
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 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 |
/* Code By Webdevtrick ( https://webdevtrick.com ) */ @import url('https://fonts.googleapis.com/css?family=Lato:100,300,400,700,900'); @import url('https://fonts.googleapis.com/css?family=Open+Sans:400,300,600,700,800'); @import url('https://fonts.googleapis.com/css?family=Montserrat:400,700'); h1 { font: 100 4em/1em Lato; margin: 0; text-transform: uppercase; } body { font: 300 16px/1.5em "Open Sans"; } form { margin: 0 auto; width: 400px; margin-top: 5%; } input.button { box-sizing: border-box; border: 0; outline: 0; height: 48px; } .button { display: inline-block; font: 100 1.2em/16px "Lato"; letter-spacing: 0.75px; text-transform: uppercase; text-align: center; height: 16px; margin: 8px 0 8px 8px; padding: 16px 24px; background: #404040; color: white; border-radius: 3px; box-shadow: 1px 1px 3px rgba(0,0,0,0.25); transition: background-color .1s, color .2s, box-shadow .3s; } .button:hover { background: #2bb673; color: white; cursor: pointer; cursor: hand; box-shadow: 1px 1px 6px rgba(0,0,0,0.65); } .fields { position: relative; font: 300 1em/48px "Open Sans"; margin: 0 0 8px; padding: 0; border-bottom: 1px solid #c0c0c0; border-radius: 4px; overflow: hidden; color: #505050; } .fields:hover { color: black; } .fields i { display: block; position: absolute; z-index: -1; top: 0; left: 0; width: 48px; height: 47px; line-height: 48px; text-align: center; /*background-color: #e0e0e0; box-shadow: 1px 1px 1px rgba(0,0,0,0.25);*/ font-size: 1em; transition: box-shadow .2s, color .3s, background-color .4s; } .fields input { box-sizing: border-box; z-index: 1; display: block; position: relative; top: 0; left: 0; float: left; font-size: 1em; font: inherit; border: 0; margin: 0; padding: 0 4px 0 56px; border: 1px solid transparent; border-radius: inherit; width: 100%; height: 47px; line-height: 47px; background: transparent; color: inherit; transition: border-color .2s, color .2s, background-color .4s; } .fields.hasTextarea { line-height: 24px; height: 191px; } .fields.hasTextarea i { border-radius: 0 0 3px; } .fields textarea { box-sizing: border-box; resize: none; z-index: 1; display: block; position: relative; top: 0; left: 0; float: left; font-size: 1em; font: inherit; border: 1px solid transparent; border-radius: inherit; margin: 0; padding: 12px 4px 6px 56px; width: 100%; height: 100%; line-height: 24px; background: transparent; color: inherit; transition: border-color .2s, color .2s, background-color .4s; } .fields input[disabled]:active,.fields input[disabled]:focus, .fields input[disabled]:hover { border-color: transparent; background-color: transparent; } .fields input:active, .fields input:focus, .fields textarea:active, .fields textarea:focus, .fields.dateSelector:hover input{ border-color: #2bb673; outline: none; color: black; background-color: rgba(0,0,0,0.025); } .fields.radioBtn { padding-bottom: 24px; } .fields.radioBtn .fieldsChild { font: 300 1em/28px "Open Sans"; } .fields.radioBtn input { width: 14px; height: 14px; padding: 0; margin: 7px 9px; position: relative; } .fields.radioBtn label { font-size: 0.95em; } .fields .fieldsDescription { position: relative; padding: 0 0 0 56px; } .fields .fieldsChild { font: 300 1em/48px "Open Sans"; position: relative; padding: 0 0 0 56px; } .fields.checkboxBtn { } .fields.checkboxBtn input { width: 14px; height: 14px; margin: 17px 16px 16px; padding: 0; } .fields.checkboxBtn i { display: none; } .fields.dateSelector { transition: background-color .2s; } .calendar { position: relative; display: block; font: 500 0.8em/30px "Open Sans"; -webkit-touch-callout: none; -webkit-user-select: none; -khtml-user-select: none; -moz-user-select: none; -ms-user-select: none; user-select: none; cursor: default; padding: 0 0 0 57px; color: #404040; } .calendar .view { position: relative; float: left; } .calendar .nav { position: absolute; top: 0; left: 0; width: 100%; } .calendar .nav .btn { position: absolute; display: block; z-index: 1; cursor: pointer; cursor: hand; width: 32px; height: 32px; line-height: 31px; top: 0; left: auto; transition: opacity .5s; border-radius: 4px; } .calendar .nav .btn:hover { color: #808080; background: #e0e0e0; border-color: #606060; } .calendar .nav .prev { left: 0; } .calendar .nav .prev-year { opacity: 0; left: 32px; } .calendar .nav:hover .prev-year { opacity: 1; } .calendar .nav .next-year { opacity: 0; right: 32px !important; } .calendar .nav:hover .next-year { opacity: 1; } .calendar .nav .next { right: 0; } .calendar .head { } .calendar .head .title { font: 300 1.2em/32px "Open Sans"; position: relative; float: left; width: 100%; text-align: center; } .calendar .head span { cursor: pointer; } .calendar .row { clear: both; } .calendar .row.th { font-weight: 700; text-transform: uppercase; height: 40px; } .calendar .row.th .C { border-bottom: 1px solid #c0c0c0; border-radius: 0; } .calendar .C { text-align: center; width: 40px; height: 40px; line-height: 40px; float: left; transition: background-color .05s, color .05s; border-radius: 50%; } .calendar .C.mn { cursor: pointer; cursor: hand; } .calendar .C.mn:hover { background: #2bb673; color: white; } .calendar .C.g { color: #a0a0a0; font-weight: 300; } .calendar .C.slctd { background: #202020; color: white; font-size: 1.15em; font-weight: 300; } .calendar .C.today { font-weight: 900; font-size: 1.3em; } .calendar .selected { margin: 0 0 0 64px; float: right; text-align: center; width: 280px; } .calendar .selected .desc { border-bottom: 1px solid #c0c0c0; } .calendar .selected .val { padding: 0 8px; background: #f0f0f0; color: #808080; } .calendar .dayArea { position: relative; width: 280px; height: 240px; overflow: hidden; } .calendar .dayArea .mArea { position: absolute; top: 0; left: 0; width: 100%; height: 100%; } |
function.js
The final step, Create a JavaScript file named ‘function.js‘ and put the codes.
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 |
// Code By Webdevtrick ( https://webdevtrick.com ) var monthNames = { 0: "January", 1: "February", 2: "March", 3: "April", 4: "May", 5: "June", 6: "July", 7: "August", 8: "September", 9: "October", 10: "November", 11: "December" } function Cal() { this.date = {}; this.markup = {}; this.date.today = new Date(); this.date.today = new Date(this.date.today.getUTCFullYear(),this.date.today.getUTCMonth(),this.date.today.getUTCDate()); this.date.browse = new Date(); this.markup.row = "row"; this.markup.cell = "cell"; this.markup.inactive = "g"; this.markup.currentMonth = "mn"; this.markup.slctd = "slctd"; this.markup.today = "today"; this.markup.dayArea = "dayArea"; this.elementTag = 'calendar'; this.targetInput = '#DOB'; this.init = false; this.buildDOM(); this.selectDate(this.date.today.getFullYear(),this.date.today.getMonth(),this.date.today.getDate()) this.constructDayArea(); this.updateInput('Birth date','',''); t = this; $(document).ready(function(){ $(document).click(function(ms){ e = $('.'+t.elementTag+' .view'); eco = e.offset(); if(ms.pageX<eco.left || ms.pageX>eco.left+e.width() || ms.pageY<eco.top || ms.pageY>eco.top+e.height()) { if(!t.init) t.hide(300); } }); $('.'+t.elementTag).on('click','.next-month',function(){ t.setMonthNext(); }); $('.'+t.elementTag).on('click','.prev-month',function(){ t.setMonthPrev(); }); $('.'+t.elementTag).on('click','.next-year',function(){ t.setYearNext(); }); $('.'+t.elementTag).on('click','.prev-year',function(){ t.setYearPrev(); }); $('.'+t.elementTag).on('click','.jump-to-next-month',function(){ t.setMonthNext(); }); $('.'+t.elementTag).on('click','.jump-to-previous-month',function(){ t.setMonthPrev(); }); $('.'+t.elementTag).on('click','.'+t.markup.currentMonth,function(){ d = t.selectDate(t.date.browse.getUTCFullYear(),t.date.browse.getUTCMonth(),$(this).html()); t.hide(300); }); $('.'+t.elementTag).on('click','.title',function(){ t.date.browse = new Date(t.date.today.getTime()); t.constructDayArea(false); }); $(t.targetInput).focus(function(){ t.show(100); $(this).blur(); }); }); } Cal.prototype.wd = function(wd) { if(wd==0) return 7 return wd } Cal.prototype.buildDOM = function() { html = "<div class='clear "+this.elementTag+"'>\n<div class='view'>\n<div class='head'>\n<div class='title'><span class='m'></span> <span class='y'></span></div>\n</div>\n"; html += "<div class='row th'>\n<div class='C'>M</div>\n<div class='C'>T</div>\n<div class='C'>W</div>\n<div class='C'>T</div>\n<div class='C'>F</div>\n<div class='C'>S</div>\n<div class='C'>S</div>\n</div>\n<div class='"+this.markup.dayArea+"'>\n"; html += "</div>\n\n<div class='row nav'>\n\n<i class='btn prev prev-year fa fa-fast-backward'></i>\n<i class='btn prev prev-month fa fa-play fa-flip-horizontal'></i>\n<i class='btn next next-month fa fa-play'></i>\n<i class='btn next next-year fa fa-fast-forward'></i>\n</div>\n</div>\n</div>\n"; $(html).insertAfter(this.targetInput); $(this.targetInput).css('cursor','pointer'); this.hide(0); } Cal.prototype.constructDayArea = function(flipDirection) { newViewContent = ""; wd = this.wd(this.date.browse.getUTCDay()); d = this.date.browse.getUTCDate(); m = this.date.browse.getUTCMonth(); y = this.date.browse.getUTCFullYear(); monthBgnDate = new Date(y,m,1); monthBgn = monthBgnDate.getTime(); monthEndDate = new Date(this.getMonthNext().getTime()-1000*60*60*24); monthEnd = monthEndDate.getTime(); monthBgnWd = this.wd(monthBgnDate.getUTCDay()); itrBgn = monthBgnDate.getTime()-(monthBgnWd-1)*1000*60*60*24; /*itrEnd = monthEnd; i = 0; while(this.wd(new Date(itrEnd).getUTCDay())!=7) { itrEnd += 1000*60*60*24; i = i+1; if(i>10) break; }*/ i = 1; n = 0; dayItr = itrBgn; newViewContent += "<div class='"+this.markup.row+"'>\n"; while(n<42) { cls = new Array("C",this.markup.cell); if(dayItr<=monthBgn) cls.push(this.markup.inactive,"jump-to-previous-month"); else if(dayItr>=monthEnd+1000*60*60*36) cls.push(this.markup.inactive,"jump-to-next-month"); else cls.push(this.markup.currentMonth); if(dayItr==this.date.slctd.getTime()+1000*60*60*24) cls.push(this.markup.slctd); if(dayItr==this.date.today.getTime()+1000*60*60*24) cls.push(this.markup.today); date = new Date(dayItr); newViewContent += "<div class='"+cls.join(" ")+"'>"+date.getUTCDate()+"</div>\n"; i += 1; if(i>7) { i = 1; newViewContent += "</div>\n<div class='"+this.markup.row+"'>\n"; } n += 1; dayItr = dayItr+1000*60*60*24; } newViewContent += "</div>\n"; this.changePage(newViewContent,flipDirection); $('.'+this.elementTag+' .title .m').html(monthNames[m]); $('.'+this.elementTag+' .title .y').html(y); return newViewContent; } Cal.prototype.changePage = function(newPageContent,flipDirection) { multiplier = -1; mark = "-"; if(flipDirection) { multiplier = 1; mark = "+"; } oldPage = $('.'+this.elementTag+' .'+this.markup.dayArea+' .mArea'); newPage = $("<div class='mArea'></div>").css('left',(-1*multiplier*224)+'px').html(newPageContent); $('.'+this.elementTag+' .'+this.markup.dayArea).append(newPage); $('.mArea').stop(1,1).animate({ left: mark+"=224px" },300,function(){ oldPage.remove(); }); } Cal.prototype.selectDate = function(y,m,d) { this.date.slctd = new Date(y,m,d); this.updateInput(y,m,d); this.constructDayArea(false); return this.date.slctd; } Cal.prototype.updateInput = function(y,m,d) { if(m=='') m = ''; else m = monthNames[m]; $(this.targetInput).val(y+" "+m+" "+d); } Cal.prototype.getMonthNext = function() { m = this.date.browse.getUTCMonth(); y = this.date.browse.getUTCFullYear(); if(m+1>11) return new Date(y+1,0); else return new Date(y,m+1); } Cal.prototype.getMonthPrev = function() { m = this.date.browse.getUTCMonth(); y = this.date.browse.getUTCFullYear(); if(m-1<0) return new Date(y-1,11); else return new Date(y,m-1); } Cal.prototype.setMonthNext = function() { m = this.date.browse.getUTCMonth(); y = this.date.browse.getUTCFullYear(); if(m+1>11) { this.date.browse.setUTCFullYear(y+1); this.date.browse.setUTCMonth(0); } else { this.date.browse.setUTCMonth(m+1); } this.constructDayArea(false); } Cal.prototype.setMonthPrev = function() { m = this.date.browse.getUTCMonth(); y = this.date.browse.getUTCFullYear(); if(m-1<0) { this.date.browse.setUTCFullYear(y-1); this.date.browse.setUTCMonth(11); } else { this.date.browse.setUTCMonth(m-1); } this.constructDayArea(true); } Cal.prototype.setYearNext = function() { y = this.date.browse.getUTCFullYear(); this.date.browse.setUTCFullYear(y+1); this.constructDayArea(false); } Cal.prototype.setYearPrev = function() { y = this.date.browse.getUTCFullYear(); this.date.browse.setUTCFullYear(y-1); this.constructDayArea(true); } Cal.prototype.hide = function(duration) { $('.'+this.elementTag+' .view').slideUp(duration); } Cal.prototype.show = function(duration) { t = this; t.init = true; $('.'+this.elementTag+' .view').slideDown(duration,function(){ t.init = false; }); } var c = new Cal(); |
That’s It. Now you have successfully created JavaScript Datepicker With jQuery and CSS UI, Form Datepicker JS Program. If you have any doubt or question comment down below.
Thanks For Visiting, Keep Visiting.