How we can create a dynamic calendar using only jQuery without any plugin? Solution: See this jQuery Dynamic Calendar Program With CSS, Basic jQuery Calendar.
Previously I have shared an animated HTML CSS JS Calendar program, but this is a dynamic calendar based on jQuery. Basically, a dynamic calendar is completely created with JS or JS library. When we don’t create HTML layout like table or list for creating the calendar, then its called a dynamic calendar. This specific calendar is completely created by jQuery, but the fact is jQuery adding here HTML elements dynamically.
Today you will learn to create Basic jQuery Calendar. Basically, there is a simple calendar with week names in the heading section and dates in the table body section. On the middle top you will see month and year information, and on the top two sides left and right there are two buttons for next and previous month. These two buttons have next and previous month’s name where you can select your month and year, not only next-previous text.
So, Today I am sharing the jQuery Dynamic Calendar Program With CSS. There I have used HTML to create only a few elements, CSS for styling, and jQuery for complete program and creating all calendar items. This is a complete calendar with all basic requirements, you can customize the style and functions of its. You can use this calendar program on your website or project.
If you are thinking now how this basic calendar actually is, then see the preview given below.
Preview Of Basic Calendar UI
See this video preview to getting an idea of how this basic calendar looks like.
Now you can see this program visually, also you can see it live by pressing the button given above. If you like this, then get the source code of its.
You May Also Like:
- Animated Navigation Menu
- JavaScript Adblock Detection
- Folding Select Dropdown
- Minimal jQuery UI Tabs
jQuery Dynamic Calendar Source Code
Before sharing source code, let’s talk about it. First I have created the header section using HTML <header> tag and div outside the header for calendar items. Inside the header, I have placed two buttons and two headings, buttons for Next and Previous months and headings for show Year and Month. Also in the HTML file, I have linked other files like CSS, JS, jQuery CDN.
Now using CSS I have placed all the items in the right place, as you can see in the preview. With CSS first I gave basic values like size, position, margin, padding, etc to all the elements. There I have styled all the elements which are created using HTML and jQuery also. There I have hidden the calendar on an extra small screen, because it breaks the layout you need a 400px or above screen width to see the program.
jQuery handling here the whole functions in the program. First I stored all weeks name, dates 1 to 31, full-year name, and short year name in the array. Then I placed the dates using for loop for() command, it makes the work easier. After that, created HTML elements dynamically and used if{} else{} statements to declare conditions.
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 ‘index.html‘ 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 |
<!DOCTYPE html> <!--Code BY Webdevtrick ( https://webdevtrick.com )--> <html lang="en" > <head> <meta charset="UTF-8"> <title>jQuery Calendar | Webdevtrick.com</title> <link rel="stylesheet" href="style.css"> </head> <body> <header> <div class="title-content"> <button id="last"></button><span> <h1 id="month"></h1> <h2 id="year"></h2></span> <button id="next"></button> </div> </header> <div class="ci-cal-container" id="calendar"></div> <script src='https://code.jquery.com/jquery-2.2.4.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 |
/* Code BY Webdevtrick ( https://webdevtrick.com ) */ *, *:after, *:before { -webkit-box-sizing: border-box; -moz-box-sizing: border-box; box-sizing: border-box; } :after, :before { content: '' } body { margin: 0; padding: 0; overflow: hidden; font-family: sans-serif; } header { top: 0; position: absolute; width: 100%;` height: 64px; } header .title-content { position: absolute; width: 100%; height: 100px; text-align: center; } header .title-content button { position: absolute; top: 12px; height: 40px; text-align: center; width: 140px; } header .title-content button#last { left: 12px; } header .title-content button#next { right: 12px; } header .title-content span { float: left; margin: 8px 0 16px 0px; width: 100%; height: 40px; } header .title-content span h1, header .title-content span h2 { clear: both; float: left; margin: 0; padding: 0; line-height: 1; text-align: center; width: 100%; } header .title-content span h1 { font-size: 24px; line-height: 32px; } header .title-content span h2 { font-size: 14px; } .ci-cal-container { top: 64px; position: absolute; border: 1px solid #CCC; width: 100%; height: calc(100% - 64px);} .ci-cal-container .ci-head { top: 0; background: #2bb673; position: absolute; width: 100%; height: 40px; } .ci-cal-container .ci-head div { line-height: 40px; float: left; color: #FFFFFF; font-size: 12px; font-weight: bold; text-align: center; border-right: 1px solid rgba(255,255,255,0.15); text-transform: uppercase; height: 100%; width: 14.28%; width: -moz-calc(100%/7); width: -webkit-calc(100%/7); width: calc(100%/7); position: relative; } .ci-cal-container .ci-head div:last-child { border-right: 0px;} .ci-cal-container .ci-body { top: 40px; position: absolute; width: 100%; height: calc(100% - 40px); } .ci-cal-container .ci-body .ci-row { clear: both; float: left; position: relative; border-bottom: 1px solid #CCC; width: 100%; height: 16.6%} .ci-cal-container .ci-calendar.cal-rows-four .ci-body .ci-row { width: 100%; height: 25%} .ci-cal-container .ci-calendar.cal-rows-five .ci-body .ci-row { width: 100%; height: 20%} .ci-cal-container .ci-calendar.cal-rows-six .ci-body .ci-row { width: 100%; height: 16.666%} .ci-cal-container .ci-body .ci-row:last-child { border-bottom: 0;} .ci-cal-container .ci-body .ci-row .ci-event-row { position: absolute; left: 0px; padding: 3px; height: 50%; width: 100%; z-index: 10;} .ci-cal-container .ci-body .ci-row .ci-event-row:nth-child(1) { top: 0px;} .ci-cal-container .ci-body .ci-row .ci-event-row:nth-child(2) { margin-top: -1px; top: 50%;} .ci-cal-container .ci-body .ci-row>div { box-shadow: inset 0px 0px 0px 1px white; border-right: 1px solid #CCC; overflow: hidden; float: left; height: 100%; width: 14.28%; width: -moz-calc(100%/7); width: -webkit-calc(100%/7); width: calc(100%/7); position: relative; } .ci-cal-container .ci-body .ci-row>div.empty { background: #f5f5f5; } .ci-cal-container .ci-body .ci-row>div.empty:before { background:url(https://webdevtrick.com/wp-content/uploads/black-twill.png); position: absolute; height: 100%; width: 100%; opacity: 0.125; } .ci-cal-container .ci-body .ci-row>div.ci-today { background: #f5f5f5; } .ci-cal-container .ci-body .ci-row>div:last-child { border-right: 0; } .ci-cal-container .ci-body .ci-row>div .ci-date { top: 12px; left: 12px; position: absolute; font-size: 12px; } .ci-event { border-radius: 3px; border: 1px solid transparent; /* box-shadow: inset 0px 0px 0px 1px white; */ position: absolute; padding: 2px 12px; height: calc(100% - 5px); z-index: 20;} .end-cap, .start-cap { top: 0.5px; z-index: 1; position: absolute; } .end-cap-border, .start-cap-border { top: -1.75px; z-index: 0; position: absolute; } .end-cap, .end-cap-border, .start-cap, .start-cap-border { width: 0px; height: 0px; border-style: solid; border-color: transparent; } .ci-event.public .end-cap, .ci-event.public .start-cap { border-color: transparent #e6f1cd; } .ci-event.public .end-cap-border, .ci-event.public .start-cap-border { border-color: transparent #D1DBBB; } .ci-event.private .end-cap, .ci-event.private .start-cap { border-color: transparent #cceff2; } .ci-event.private .end-cap-border, .ci-event.private .start-cap-border { border-color: transparent #9FBABD; } .ci-event.public { background: #e6f1cd; border-color: #D1DBBB; } .ci-event.private { background: #cceff2; border-color: #9FBABD; } .ci-event label { font-size: 12px; font-weight: bold; vertical-align: middle; } @media(max-width:360px) { body { overflow-x: hidden !important; overflow-y: auto !important; } .ci-cal-container .ci-body { float: left; height: auto; position: relative; } .ci-cal-container .ci-body .ci-row { border: 0px; height: auto !important; } .ci-cal-container .ci-body .ci-row .ci-event-row { top: 0px; clear: both; float: left; height: auto; padding: 0px !important; margin-top: 0px !important; position: relative; } .ci-cal-container .ci-body .ci-row > div { display: none; } .ci-event { float: left; position: relative; min-height: 100px; left: 0px !important; margin: 5px !important; width: calc(100% - 10px) !important; } .end-cap, .end-cap-border, .start-cap, .start-cap-border { display: none; } } |
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 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 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 |
// Code BY Webdevtrick ( https://webdevtrick.com ) ;(function($, window, undefined) { 'use strict'; $.ciCalendar = function(options, element) { this.$el = $(element); this._init(options); }; // set the calendars default options $.ciCalendar.defaults = { // array of the different days of the week // 'days' for the full string of each day // '_days' for the abbreviation of each day days : ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'], _days : ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'], // array of the different months of the year // 'months' for the full string of each month // '_months' for the abbreviation of each month months : ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'], _months : ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'], // toggle abbreviations for // how days are displayed abbr_days: false, // toggle abbreviations for // how months are displayed abbr_months: false, // set the left most day in the calendar // 0 for sunday, 1 for monday, etc. start_day: 0, events: {}, // event handler for when a day (in the current month) is clicked dateClick: function($el, $content, dateProperties){ return false; } }; // begin the prototype $.ciCalendar.prototype = { /* * _init * * Sets the default options into place * and initializes the calendar. * * @param options * */ _init: function(options) { // set the prototype options // pulls from defaults unless manually changed this.options = $.extend(true, {}, $.ciCalendar.defaults, options); // set todays date this.today = new Date(); // set the current month this.month = (isNaN(this.options.month) || this.options.month == null) ? this.today.getMonth() : this.options.month - 1; // set the current year this.year = (isNaN(this.options.year) || this.options.year == null) ? this.today.getFullYear() : this.options.year; // set the data array this.data = this.options.data || {}; // generate the calendar template this._generate(); // initialize the events this._initEvents(); }, /* * _initEvents * * Initializes global events such as * calendar cell clicks. * */ _initEvents: function() { // internalize var self = this; // handle calendar cell click events this.$el.on('click.calendar', 'div.ci-row > div', function() { var $cell = $(this), idx = $cell.index(), $content = $cell.children('div'), dateProp = { day: $cell.children('span.ci-date').text(), month: self.month + 1, monthname: self.options.abbr_months ? self.options._months[self.month] : self.options.months[self.month], year: self.year, weekday: idx + self.options.start_day - 2, weekdayname: self.options.days[idx + self.options.start_day - 2] }; if(dateProp.day) { self.options.dateClick($cell, $content, dateProp); } }); }, /* * _generate * * Generates a fresh view of the calendar * based on the current year and month. * * @param callback * @return dom * */ _generate: function(callback) { var head = this._getHead(), body = this._getDays(), rowClass; switch(this.rowTotal) { case 4 : rowClass = 'cal-rows-four'; break; case 5 : rowClass = 'cal-rows-five'; break; case 6 : rowClass = 'cal-rows-six'; break; } this.$cal = $('<div class="ci-calendar ' + rowClass + '">').append(head, body); this.$el.find('div.ci-calendar').remove().end().append(this.$cal); if(callback) { callback.call(); } }, /* * _getHead * * Generates the weekday strings in the header * of the calendar. Order of weekdays is based * on the options.start_day parameter. * * @return html * */ _getHead: function() { var html = '<div class="ci-head">'; for(var i = 0; i <= 6; i++) { var pos = i + this.options.start_day, j = pos > 6 ? pos - 6 - 1 : pos; html += '<div>'; html += this.options.abbr_days ? this.options._days[j] : this.options._days[j]; html += '</div>'; } html += '</div>'; return html; }, /* * _getDays * * Generates the rest of the calendar. * Creates 6 rows, each containing 7 cells. * Cross-checks which day the month starts * and ends on and fills the cells accordingly. * * @return html * */ _getDays: function() { var d = new Date(this.year, this.month + 1, 0), monthLength = d.getDate(), firstDay = new Date(this.year, this.month, 1); // get the starting dat of the month this.startingDay = firstDay.getDay(); // start creating the html output for the calendar cells var html = '<div class="ci-body" data-month="' + (this.month + 1) + '"><div class="ci-row" data-week="1">', day = 1; // loop through the weekdays for(var i = 0; i < 7; i++) { // add containers for events for(var k = 0; k < 2; k++) { html += '<span class="ci-event-row" data-event-row="' + (k+1) + '"></span>'; } // loop through week rows for(var j = 0; j <= 6; j++) { var pos = this.startingDay - this.options.start_day, p = pos < 0 ? 6 + pos + 1 : pos, inner = '', today = this.month === this.today.getMonth() && this.year === this.today.getFullYear() && day === this.today.getDate(), content = ''; if(day <= monthLength && (i > 0 || j >= p)) { html += '<div class="' + cellClasses + '" data-month="' + (this.month + 1) + '" data-day="' + day + '" data-events="0">'; inner += '<span class="ci-date">' + day + '</span>'; var strdate = (this.month + 1 < 10 ? '0' + (this.month + 1) : this.month + 1) + '-' + (day < 10 ? '0' + day : day) + '-' + this.year, dayData = this.data[strdate]; if(dayData) { content = dayData; } if(content !== '') { inner += '<div class="empty">' + content + '</div>'; } ++day; }else{ html += '<div class="empty">'; today = false; } var cellClasses = today ? 'ci-today ' : ''; if(content !== '') { cellClasses += 'ci-content '; } html += inner; html += '</div>'; } if(day > monthLength) { this.rowTotal = i + 1; break; }else{ html += '</div><div class="ci-row" data-week="' + (i+2) + '">'; } } html += '</div></div>'; return html; }, /* * _isValidDate * * Referenced from http://stackoverflow.com/a/8390325/989439 * * Let's make sure the date being passed through is valid. * Checks day values, month values, leap years, etc. * * @param date * @return array * */ _isValidDate: function(date) { // change date to 'MMDDYYYY' format date = date.replace(/-/gi, ''); // seperate the date string into vars var month = parseInt(date.substr(0, 2), 10), day = parseInt(date.substr(2, 4), 10), year = parseInt(date.substr(4, 8), 10); // is the month between 1 and 12? if((month < 1) || (month > 12)) { return false; // is the dat between 1 and 31? }else if((day < 1) || (day > 31)) { return false; // there are only 30 days in April, June, September, and November }else if(((month == 4) || (month == 6) || (month == 9) || (month == 11)) && (day > 30)) { return false; // check for leap years }else if((month == 2) && (((year % 400) == 0) || ((year % 4) == 0)) && ((year % 100) != 0) && (day > 29)) { return false; // double check for leap years }else if((month == 2) && ((year % 100) == 0) && (day > 29)) { return false; } return { day: day, month: month, year: year }; }, /* * _clearEvents * * Move the calendar between months and years. * The movement is based on the period and dir * values that are passed into the function. * Period values can be either 'month' or 'year' * and the Dir values 'previous' or 'next'. * */ _clearEvents: function() { $('.ci-event').remove(); $('.ci-row > div').attr('data-events', 0); }, /* * _updateEvents * * Move the calendar between months and years. * The movement is based on the period and dir * values that are passed into the function. * Period values can be either 'month' or 'year' * and the Dir values 'previous' or 'next'. * * @param array * */ _updateEvents: function(_events) { this._clearEvents(); // console.log(_events); // calculate the number of days between // two supplied dates in mm/dd/yyyy format Date.prototype.diffDates = function() { var mill = 24 * 60 * 60 * 1000, diff = arguments[0] - this, days = Math.floor(diff/mill); return days; // add 1 to include the first day } // loop through each event supplied in // the _events object when the function is called $.each(_events, function(index, _event) { // define the dom elements we'll need var $_event_start_day = $('.ci-row > div[data-day="' + _event.start['day'] + '"]'), $_event_finish_day = $('.ci-row > div[data-day="' + _event.end['day'] + '"]'), $_event_start_week = $_event_start_day.parent(), $_event_finish_week = $_event_finish_day.parent(), $_event_start_cells = new Array(), $_event_finish_cells = new Array(); // define the start date, end date, and number days between them var _dateStart = new Date(_event.start['month'] + '/' + _event.start['day'] + '/' + _event.start['year']), _dateFinish = new Date(_event.end['month'] + '/' + _event.end['day'] + '/' + _event.end['year']), _startPos = $_event_start_day.index() - 1, _finishPos = $_event_finish_day.index() - 1, _totalDays = _dateStart.diffDates(_dateFinish); // define the widths of needed elements var _blockWidth = Math.floor($('.ci-row > div').width()), // width of each day in pixels _eventWidth = _blockWidth * _totalDays, // total width of the event in pixels _eventHeight = Math.floor(($('.ci-event-row').outerHeight() / 2) - 4.5), // get the events height _daysWidth = _blockWidth * 7, // total width of the calendar in pixels _eventOffset = _startPos * _blockWidth - _blockWidth, _eventTotals = _eventWidth + _eventOffset, _eventLeftover = _eventTotals - _daysWidth; $_event_start_day.addClass('start'); // make sure the event matches the currently // viewed year and month before appending to calendar if(_event.start['year'] == calendar.year && _event.start['month'] == calendar.month + 1) { for(var i = _event.start['day']; i <= _event.end['day']; i++){ var j = $('.ci-row > div[data-day="' + i + '"]').attr('data-events'); j++; $('.ci-row > div[data-day="' + i + '"]').attr('data-events', j); } if(_eventTotals > _daysWidth) { // this event is long and will carry into the next row $_event_start_week.find('.ci-event-row').each(function(i, el) { $_event_start_cells.push(el); }); $_event_finish_week.find('.ci-event-row').each(function(i, el) { $_event_finish_cells.push(el); }); var _eventDifference = _eventTotals - _daysWidth, _eventToDraw = _eventTotals - _eventOffset - _eventDifference; var html_one = '<span class="ci-event private'; html_one += '" style="left:' + (_eventOffset + 6) + 'px;'; html_one += 'width:' + (_eventToDraw - _eventHeight - 3) + 'px;">'; html_one += '<span class="end-cap" style="'; html_one += 'right:-' + Math.floor(_eventHeight/1.5) + 'px; border-width: ' + _eventHeight + 'px 0px ' + _eventHeight + 'px ' + Math.floor(_eventHeight / 1.5) + 'px;'; html_one += '"></span>'; html_one += '<span class="end-cap-border" style="'; html_one += 'right:-' + Math.floor((_eventHeight/1.5) + 2) + 'px; border-width: ' + (_eventHeight + 2) + 'px 0px ' + (_eventHeight + 2) + 'px ' + Math.floor((_eventHeight / 1.5) + 2) + 'px;'; html_one += '"></span>'; html_one += '<label>' + _event.details['title'] + '<br>starts: ' + _event.start['month'] + '/' + _event.start['day'] + ', ends: ' + _event.end['month'] + '/' + _event.end['day'] + '...</label>'; html_one += '</span>'; if($_event_start_day.attr('data-events') <= 1) { $($_event_start_cells[0]).append(html_one); }else if($_event_start_day.attr('data-events') >= 1) { $($_event_start_cells[1]).append(html_one); }else if($_event_start_day.attr('data-events') > 1) { console.log('view more'); } if(_event.end['month'] === _event.start['month']) { var html_two = '<span class="ci-event private'; html_two += '" style="left:' + (_eventHeight + 6) + 'px;'; html_two += 'width:' + ((_eventDifference - _eventHeight + _blockWidth) - 9) + 'px;">'; html_two += '<span class="start-cap" style="'; html_two += 'left:-' + Math.floor(_eventHeight/1.5) + 'px; border-width: ' + _eventHeight + 'px ' + Math.floor(_eventHeight / 1.5) + 'px ' + _eventHeight + 'px 0px;'; html_two += '"></span>'; html_two += '<span class="start-cap-border" style="'; html_two += 'left:-' + Math.floor((_eventHeight/1.5) + 2) + 'px; border-width: ' + (_eventHeight + 2) + 'px ' + Math.floor((_eventHeight / 1.5) + 2) + 'px ' + (_eventHeight + 2) + 'px 0px;'; html_two += '"></span>'; html_two += '<label>...' + _event.details['title'] + '<br>starts: ' + _event.start['month'] + '/' + _event.start['day'] + ', ends: ' + _event.end['month'] + '/' + _event.end['day'] + '</label>'; html_two += '</span>'; if($_event_start_day.attr('data-events') <= 1) { $($_event_finish_cells[0]).append(html_two); }else if($_event_start_day.attr('data-events') >= 1) { $($_event_finish_cells[1]).append(html_two); }else if($_event_start_day.attr('data-events') > 1) { console.log('view more'); } } }else{ // this event fits within its starting row $_event_start_week.find('.ci-event-row').each(function(i, el) { $_event_start_cells.push(el); }); var html = '<span class="ci-event public'; html += '" style="left:' + (_eventOffset + 6) + 'px;'; if(_event.start['month'] != _event.end['month']) { html += 'width:' + (_eventWidth - _eventHeight + _blockWidth - 5) + 'px;">'; html += '<span class="end-cap" style="'; html += 'right:-' + Math.floor(_eventHeight/1.5) + 'px; border-width: ' + _eventHeight + 'px 0px ' + _eventHeight + 'px ' + Math.floor(_eventHeight / 1.5) + 'px;'; html += '"></span>'; html += '<span class="end-cap-border" style="'; html += 'right:-' + Math.floor((_eventHeight/1.5) + 2) + 'px; border-width: ' + (_eventHeight + 2) + 'px 0px ' + (_eventHeight + 2) + 'px ' + Math.floor((_eventHeight / 1.5) + 2) + 'px;'; html += '"></span>'; }else{ html += 'width:' + (_eventWidth + _blockWidth - 5) + 'px;">'; } html += '<label>' + _event.details['title'] + '<br>starts: ' + _event.start['month'] + '/' + _event.start['day'] + ', ends: ' + _event.end['month'] + '/' + _event.end['day'] + '</label>'; html += '</span>'; if($_event_start_day.attr('data-events') <= 1) { $($_event_start_cells[0]).append(html); }else if($_event_start_day.attr('data-events') >= 1) { $($_event_start_cells[1]).append(html); }else if($_event_start_day.attr('data-events') > 1) { console.log('view more'); } } }else if(_event.end['year'] == calendar.year && _event.end['month'] == calendar.month + 1) { // this event is being carried over from the previous month $_event_finish_week.find('.ci-event-row').each(function(i, el) { $_event_finish_cells.push(el); }); var _newStartPos = _finishPos - _totalDays > 1 ? _finishPos - _totalDays : 1, _newStartPos = _newStartPos * _blockWidth - _blockWidth, _newEventWidth = _blockWidth * _finishPos - _blockWidth; var html = '<span class="ci-event public'; html += '" style="left:' + (_newStartPos + _eventHeight + 6) + 'px;'; html += 'width:' + ((_newEventWidth - _eventHeight) - 5) + 'px;">'; html += '<span class="start-cap" style="'; html += 'left:-' + Math.floor(_eventHeight/1.5) + 'px; border-width: ' + _eventHeight + 'px ' + Math.floor(_eventHeight / 1.5) + 'px ' + _eventHeight + 'px 0px;'; html += '"></span>'; html += '<span class="start-cap-border" style="'; html += 'left:-' + Math.floor((_eventHeight/1.5) + 2) + 'px; border-width: ' + (_eventHeight + 2) + 'px ' + Math.floor((_eventHeight / 1.5) + 2) + 'px ' + (_eventHeight + 2) + 'px 0px;'; html += '"></span>'; html += '<label>' + _event.details['title'] + '<br>starts: ' + _event.start['month'] + '/' + _event.start['day'] + ', ends: ' + _event.end['month'] + '/' + _event.end['day'] + '</label>'; html += '</span>'; $($_event_finish_cells[0]).append(html); } }); }, /* * _move * * Move the calendar between months and years. * The movement is based on the period and dir * values that are passed into the function. * Period values can be either 'month' or 'year' * and the Dir values 'previous' or 'next'. * * @param period * @param dir * @param callback * */ _move: function(period, dir, callback) { // take a step back if(dir === 'previous') { // go back a month if(period === 'month') { if(this.month > 0) { this.month = --this.month; }else{ this.month = 11; this.year = --this.year; } // go back a year }else if(period === 'year') { this.year = --this.year; } // put your best foot forward }else if(dir === 'next') { // go forward a month if(period === 'month') { if(this.month < 11) { this.month = ++this.month; }else{ this.month = 0; this.year = ++this.year; } // go forward a year }else if(period === 'year') { this.year = ++this.year; } } this._generate(callback); }, /* * _getYear * * Return the current calendar year * @return int * */ _getYear: function() { return this.year; }, /* * _getMonth * * Return the current calendar month * @return int * */ _getMonth: function() { return this.month + 1; }, _getNextMonth: function() { var next_month = this.month; if(next_month < 11) { next_month = ++next_month; }else{ next_month = 0; } return this.options.abbr_months ? this.options._months[next_month] : this.options.months[next_month]; }, _getLastMonth: function() { var last_month = this.month; if(last_month > 0) { last_month = --last_month; }else{ last_month = 11; } return this.options.abbr_months ? this.options._months[last_month] : this.options.months[last_month]; }, /* * _getMonthName * * Return the current calendar month * as a string value. Output will be * determined based on abbreviations * being enabled or disabled. * * @return string * */ _getMonthName: function() { return this.options.abbr_months ? this.options._months[this.month] : this.options.months[this.month]; }, _getCell: function(day) { var row = Math.floor((day + this.startingDay - this.options.start_day) / 7), pos = day + this.startingDay - this.options.start_day - (row * 7) - 1; return this.$cal.find('div.ci-body').children('div.ci-row').eq(row).children('div').eq(pos).children('div'); }, _setData: function(data) { data = data || {}; $.extend(this.data, data); this._generate(); }, /* * _gotoNow * * Update the month and year values * to the current date then regenerate * the calendar view. * * @param callback * */ _gotoNow: function(callback) { this.month = this.today.getMonth(); this.year = this.today.getFullYear(); this._generate(callback); }, /* * _gotoDate * * Update the calendar to display a * specific month and year based on * the passed values. * * @param month * @param year * @param callback * */ _gotoDate: function(month, year, callback) { this.month = month; this.year = year; this._generate(callback); }, /* * _gotoNextMonth * * Move the current calendar view * to the upcoming month and then * regenerate the view. * * @param callback * */ _gotoNextMonth: function(callback) { this._move('month', 'next', callback); }, /* * _gotoPreviousMonth * * Move the current calendar view * to the previous month and then * regenerate the view. * * @param callback * */ _gotoPreviousMonth: function(callback) { this._move('month', 'previous', callback); }, /* * _gotoNextYear * * Move the current calendar view * to the upcoming year and then * regenerate the view. * * @param callback * */ _gotoNextYear: function(callback) { this._move('year', 'next', callback); }, /* * _gotoPreviousYear * * Move the current calendar view * to the previous year and then * regenerate the view. * * @param callback * */ _gotoPreviousYear: function(callback) { this._move('year', 'previous', callback); } }; /* * _errors * * Basic error logging. Passed errors * are logged through the browser console. * * @param message * @return error * */ var _errors = function(message) { if(window.console) { window.console.error(message); } }; /* * ciCalendar * * * @param options * @return instance * */ $.fn.ciCalendar = function(options) { // begin a new instance of the calendar var instance = $.data(this, 'ciCalendar'); // if the options being passed into the calendar // are in string format, let's go ahead and split // it up into a nice shiny array if(typeof options == 'string') { // shiny new array of arguments var args = Array.prototype.slice.call(arguments, 1); // loop the arguments this.each(function() { if(!instance) { _errors('cannot call methods on the calendar prior to initialization. ' + 'attempted to call method "' + options + '"'); return; } if(!$.isFunction(instance[options]) || options.charAt(0) === '_') { _errors('no such method "' + options + '" for the calendar instance'); return; } // valid arguments passed as options instance[options].apply(instace, args); }); }else{ // the options are not being passed as a // string. let's parse the passed array // and apply them as options // loop the arguments this.each(function() { if(instance) { // initialize the existing // instance that was passed instance._init(); }else{ // create a new instance // and initialize it afterward instance = $.data(this, 'ciCalendar', new $.ciCalendar(options, this)); } }); } return instance; }; })(jQuery, window); var calendar = $('#calendar').ciCalendar({ dateClick: function($el, $contentEl, dateProperties) { console.clear(); for(var key in dateProperties) { console.log(key + ' = ' + dateProperties[key]); } } }), $month = $('#month').html(calendar._getMonthName()), $year = $('#year').html(calendar._getYear()), $last = $('#last').html(calendar._getLastMonth()), $next = $('#next').html(calendar._getNextMonth()); var events = [ event = { details: { title: 'This is a longer event that breaks into a new row!', location: 'expo center', country: 'united states', city: 'san diego', public: true }, start: { time: '12:00pm', month: 7, day: 17, year: 2019, }, end: { time: '12:00pm', month: 7, day: 26, year: 2019, } }, event = { details: { title: 'An event that shares days as another event', location: 'expo center', country: 'united states', city: 'san diego', public: true }, start: { time: '12:00pm', month: 7, day: 20, year: 2019, }, end: { time: '12:00pm', month: 7, day: 23, year: 2019, } }, event = { details: { title: 'This is a shorter single row event!', location: 'expo center', country: 'united states', city: 'san diego', public: true }, start: { time: '12:00pm', month: 8, day: 1, year: 2019, }, end: { time: '12:00pm', month: 8, day: 3, year: 2019, } }, event = { details: { title: 'Another short event sharing days', location: 'expo center', country: 'united states', city: 'san diego', public: true }, start: { time: '12:00pm', month: 8, day: 2, year: 2019, }, end: { time: '12:00pm', month: 8, day: 5, year: 2019, } }, ]; calendar._updateEvents(events); $last.on('click', function() { calendar._gotoPreviousMonth(updateMonthYear); }); $next.on('click', function() { calendar._gotoNextMonth(updateMonthYear); }); $(window).on('resize', function() { calendar._updateEvents(events); }); function updateMonthYear() { $month.html(calendar._getMonthName()); $year.html(calendar._getYear()); $next.html(calendar._getNextMonth()); $last.html(calendar._getLastMonth()); calendar._updateEvents(events); } |
That’s It. Now you have successfully created jQuery Dynamic Calendar Program With CSS, Basic jQuery Calendar. If you have any doubt or question comment down below.
Thanks For Visiting, Keep Visiting.
Hi Any tutorial or example of how to use it? E.g. Adding events