From 3061827f467921a856bd4629bf21ac6cb185e4f2 Mon Sep 17 00:00:00 2001 From: Jhonis de Souza Date: Mon, 8 Feb 2016 19:04:39 -0200 Subject: [PATCH] First day of the Week #10 - The calendar now has an option (firstDayOfWeek) to determine what day is the first, this option comes from 0 to 6, in which 0 is the Sunday and 6 is Saturday. --- README.md | 6 ++++-- js/jquery.e-calendar.js | 17 +++++++++++++---- 2 files changed, 17 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index af3dbae..2f2a424 100644 --- a/README.md +++ b/README.md @@ -17,7 +17,8 @@ url: '', events: [ {title: 'Event Title 1', description: 'Description 1', datetime: new Date(2016, 0, 12, 17)}, {title: 'Event Title 2', description: 'Description 2', datetime: new Date(2016, 0, 23, 16)} -] +], +firstDayOfWeek: 0 ``` ####Event Object Properties ```JavaScript @@ -38,7 +39,8 @@ $('#calendar').eCalendar({ }); $('#calendar').eCalendar({url: 'loadCalendar', - weekDays: ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat']}); + weekDays: ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'], + firstDayOfWeek: 1}); // calendar starting on monday | (0 - 6: week days format) ``` PS: remember that in date object instantiation, like the code below, the month starts from 0 to 11 and generate a date: 31/01/2016 diff --git a/js/jquery.e-calendar.js b/js/jquery.e-calendar.js index 4a18ebe..337791b 100644 --- a/js/jquery.e-calendar.js +++ b/js/jquery.e-calendar.js @@ -87,7 +87,10 @@ function print() { loadEvents(); - var dWeekDayOfMonthStart = new Date(dYear, dMonth, 1).getDay(); + var dWeekDayOfMonthStart = new Date(dYear, dMonth, 1).getDay() - settings.firstDayOfWeek; + if (dWeekDayOfMonthStart < 0) { + dWeekDayOfMonthStart = 6 - ((dWeekDayOfMonthStart + 1) * -1); + } var dLastDayOfMonth = new Date(dYear, dMonth + 1, 0).getDate(); var dLastDayOfPreviousMonth = new Date(dYear, dMonth + 1, 0).getDate() - dWeekDayOfMonthStart + 1; @@ -109,10 +112,15 @@ cBody.append(cPrevious); cBody.append(cMonth); cBody.append(cNext); - for (var i = 0; i < settings.weekDays.length; i++) { + var dayOfWeek = settings.firstDayOfWeek; + for (var i = 0; i < 7; i++) { + if (dayOfWeek > 6) { + dayOfWeek = 0; + } var cWeekDay = $('
').addClass('c-week-day c-pad-top'); - cWeekDay.html(settings.weekDays[i]); + cWeekDay.html(settings.weekDays[dayOfWeek]); cBody.append(cWeekDay); + dayOfWeek++; } var day = 1; var dayOfNextMonth = 1; @@ -179,7 +187,8 @@ {title: 'Evento de Abertura', description: 'Abertura das Olimpíadas Rio 2016', datetime: new Date(2016, new Date().getMonth(), 12, 17)}, {title: 'Tênis de Mesa', description: 'BRA x ARG - Semifinal', datetime: new Date(2016, new Date().getMonth(), 23, 16)}, {title: 'Ginástica Olímpica', description: 'Classificatórias de equipes', datetime: new Date(2016, new Date().getMonth(), 31, 16)} - ] + ], + firstDayOfWeek: 0 }; }(jQuery)); \ No newline at end of file