2
Like highlight (stylize) the day of the month and the week?
The function below was the simplest I could get on the Web that allows me to understand at least part of the code. I need to know 1) how to apply CSS to the current day and 2) access, alter and stylize the week).
The object Date()
allows access to the year, month and day, but does not have a direct method that returns the week (series of days from Sunday to Saturday), so that I can refer to current week, previous or posterior, as is done in getDate() - 1
, for example.
To the access and change the series of days of the desired week, want style it somehow (tag <tr>
) in CSS to highlight, as in the image below. Ex: 15 to 21 July OR 29 July to 4 August.
In addition, case the previous/subsequent week for part of last month/following, would have to display the two months, side by side, (as in the image below).
Ex: If today were November 3, as highlighted in the image below, a reference to last week would belong to the month of October (days 24 to 30), making the two months in question necessary in order to display the current day and the week before.
Remarks:
To stylization of the week must be arbitration, spanning a single entire line (tag <tr>
), as in the first image (days 15-21). By arbitration I mean to be able to pass a value to the function returning 1) current week, 2) last week, 3) week before, 4) next week, or 5) week after the following week (as well as getDate() - 1
returns yesterday), regardless of the current day (24, in the first image), which serves only for reference, with the aim of identify 1 of those 5 possibilities of weeks in the Calendar. Hence the need to sometimes include a second month (2nd image), due to the difference between present day and the desired week.
The main objective is to highlight the day of the month and the line <tr>
of the desired week, whether or not this is accomplished by accessing the weeks the way I explained it.
<body>
<style>
.week {
outline: solid 1px red;
}
</style>
<div id='cal'></div>
<script type="text/javascript">
function Calendar(id, year, month) {
var elem = document.getElementById(id)
var mon = month - 1
var d = new Date(year, mon)
var weekDay = ["Sun", "Mon", "Tues", "Wed", "Thu", "Fri", "Sat"];
var months = ['January', 'Frebruary', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'];
var table = ['<table><tr>']
table.push('<th colspan = 4>' + months[mon] + '</th>' + '<th colspan = 3>' + year + '</th>' + '</tr><tr>')
for (var i = 0; i <= 6; i++ ) {
table.push('<th>' + weekDay[i] + '</th>');
}
table.push('</tr><tr>')
for (var i=0; i<d.getDay(); i++) {
table.push('<td></td>')
}
while(d.getMonth() == mon) {
table.push('<td>'+d.getDate()+'</td>')
if (d.getDay() % 7 == 6) {
table.push('</tr><tr>')
}
d.setDate(d.getDate()+1)
}
for (var i=d.getDay(); i<7; i++) {
table.push('<td></td>')
}
table.push('</tr></table>')
elem.innerHTML = table.join('\n')
}
new Calendar("cal", 2015, 2);
</script>
</body>
You can paste images directly into the body of the question. Images in external links, tend to fall over time.
– Miguel Angelo
Could show what you’ve done?
– Felipe Avelar
@Eden, optionally you can use the Momentjs blibiotace to manipulate the date: http://momentjs.com. You can view the following jsPerf: https://jsperf.com/moment-js-native-date/4
– Tobias Mesquita