Skip to content

Instantly share code, notes, and snippets.

@lucamilan
Created April 19, 2021 20:15
Show Gist options
  • Select an option

  • Save lucamilan/1bd7ff7fa9e30699270f83100d653adf to your computer and use it in GitHub Desktop.

Select an option

Save lucamilan/1bd7ff7fa9e30699270f83100d653adf to your computer and use it in GitHub Desktop.
Simple Calendar
div#main.container
div.jumbotron
h1.text-center
a#left(href="#")
i.fa.fa-chevron-left
span  
span#month
span  
span#year
span  
a#right(href="#")
i.fa.fa-chevron-right
div.row
div.col-sm-10.col-sm-offset-1
table.table
span#about
a(href="https://odran037.github.io" target="_blank")
i.fa.fa-question-circle
$(document).ready(function() {
var currentDate = new Date();
function generateCalendar(d) {
function monthDays(month, year) {
var result = [];
var days = new Date(year, month, 0).getDate();
for (var i = 1; i <= days; i++) {
result.push(i);
}
return result;
}
Date.prototype.monthDays = function() {
var d = new Date(this.getFullYear(), this.getMonth() + 1, 0);
return d.getDate();
};
var details = {
// totalDays: monthDays(d.getMonth(), d.getFullYear()),
totalDays: d.monthDays(),
weekDays: ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'],
months: ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'],
};
var start = new Date(d.getFullYear(), d.getMonth()).getDay();
var cal = [];
var day = 1;
for (var i = 0; i <= 6; i++) {
cal.push(['<tr>']);
for (var j = 0; j < 7; j++) {
if (i === 0) {
cal[i].push('<td>' + details.weekDays[j] + '</td>');
} else if (day > details.totalDays) {
cal[i].push('<td>&nbsp;</td>');
} else {
if (i === 1 && j < start) {
cal[i].push('<td>&nbsp;</td>');
} else {
cal[i].push('<td class="day">' + day++ + '</td>');
}
}
}
cal[i].push('</tr>');
}
cal = cal.reduce(function(a, b) {
return a.concat(b);
}, []).join('');
$('table').append(cal);
$('#month').text(details.months[d.getMonth()]);
$('#year').text(d.getFullYear());
$('td.day').mouseover(function() {
$(this).addClass('hover');
}).mouseout(function() {
$(this).removeClass('hover');
});
}
$('#left').click(function() {
$('table').text('');
if (currentDate.getMonth() === 0) {
currentDate = new Date(currentDate.getFullYear() - 1, 11);
generateCalendar(currentDate);
} else {
currentDate = new Date(currentDate.getFullYear(), currentDate.getMonth() - 1)
generateCalendar(currentDate);
}
});
$('#right').click(function() {
$('table').html('<tr></tr>');
if (currentDate.getMonth() === 11) {
currentDate = new Date(currentDate.getFullYear() + 1, 0);
generateCalendar(currentDate);
} else {
currentDate = new Date(currentDate.getFullYear(), currentDate.getMonth() + 1)
generateCalendar(currentDate);
}
});
generateCalendar(currentDate);
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.11.1/moment.min.js"></script>

Simple Calendar

Simple calendar - Just playing around with Javascript Date()

I wanted to create a calendar generated by Javascript that as you cycle through the months, the days are correctly placed into its corresponding weekdays.

Originally I wanted to do this with vanilla Javascript. It became too tedious and needless to say the code looked like a not so tasty plate of spaghetti.

A Pen by Leonardo Prates on CodePen.

License.

body
margin 50px
.jumbotron
background #ffffff
.fa
font-size 0.5em
table
width 100%
th
text-align center
td
width 120px
height 100px
text-align center
line-height 100px !important
font-size 1.5em
td, #year
font-family monospace
.hover
background #eeeeee
#about
font-size 5.0em !important
position absolute
top -35px
right 15px
a
text-decoration none
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css" rel="stylesheet" />
<link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.5.0/css/font-awesome.min.css" rel="stylesheet" />
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment