All times are UTC + 1 hour




Post new topic Reply to topic  [ 6 posts ] 
  Print view

calender javascript
Author Message
PostPosted: Tue Nov 17, 2015 10:26 am 
Offline

Joined: Tue Mar 17, 2015 12:24 pm
Posts: 19
Hi,
how can I customize the JavaScript-calender?
I just want Monday as first day and that I see only Monday till Friday.
Any ideas?
Thanks!


Top
 Profile  
 

Re: calender javascript
PostPosted: Sun Nov 22, 2015 9:36 pm 
Regular
Offline

Joined: Wed Aug 26, 2009 4:49 am
Posts: 353
Location: Queenstown, New Zealand
Hi
Check the JavaScript in atk/javascript/calendar/calendar.js

There are many lines like
Code:
for (var j = 0; j < 7; ++j) {
and
Code:
for (var i = 0; i < 6; ++i) {


I suspect that by changing the 7's to 5's and 6's to 4's would go a long way to giving you a 5 day week ?

There is a parameter to start the week on Monday. In atk/javascript/calendar/calendar-runner.js you need to change the first parameter of these 2 lines from false to true

Code:
    // Bind calendar to dateattribute widgets
    cal = new Calendar(false, null, changeWidget, closeHandler);
    // Bind calendar to textfield.
    cal = new Calendar(false, null, changeTextField, closeHandler);


Wayne


Top
 Profile  
 

Re: calender javascript
PostPosted: Mon Nov 23, 2015 8:15 am 
Offline

Joined: Tue Mar 17, 2015 12:24 pm
Posts: 19
Thank you für your help!
I had the Problem that I didn´t saw a result when I changed sth on the calender.js. I had to delete my temp-files and Cookies of the browser!
Now I have a much better solution:
I changed the cell.classname to "disabled"on both places where you find "weekend".
Thanks anyway! :)


Top
 Profile  
 

Re: calender javascript
PostPosted: Tue Dec 03, 2019 1:47 pm 
Offline

Joined: Tue Dec 03, 2019 1:42 pm
Posts: 2
Location: United Arab Emirates
The Same Situation happened with me and i follow the step but the load time has increased drastically. How can I Improve load time?

_________________
Digital Gravity


Top
 Profile  
 

Re: calender javascript
PostPosted: Tue Dec 03, 2019 3:31 pm 
Offline

Joined: Tue Dec 03, 2019 3:20 pm
Posts: 1
@hazrahuzaim I think this code will help you to decrease your load time.
Code:
let monthsNames = [
  "January",
  "February",
  "March",
  "April",
  "May",
  "June",
  "July",
  "August",
  "September",
  "October",
  "November",
  "December"
];
let daysInMonth = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
let daysNames = [
  "Monday",
  "Tuesday",
  "Wednesday",
  "Thursday",
  "Friday",
  "Saturday",
  "Sunday"
];

let today = new Date();
let g_currentMonth = today.getMonth();
let g_currentYear = today.getFullYear();
let g_startDay = (new Date(today.getFullYear(), today.getMonth(), 1).getDay() + 6) % 7; //first day in current month

function showCalendar(currentMonth, currentYear, startDay) {
  clearCalendarView();
  let currentDay = getCurrentDay();

  let calendar = document.createElement("table");
  calendar.classList.add("calendar");
  calendar.id = "calendar_id";

  let content = document.getElementById("content");

  let calendarHeader = createHeader();
  calendar.appendChild(calendarHeader);

  let info = createInfo(currentMonth, currentYear);
  info.id = "info_id";
  content.appendChild(info);


  let days = createDaysForMonth(monthsNames[currentMonth], daysNames[startDay], currentDay);
  calendar.appendChild(days);

  content.appendChild(calendar);
}

function getCurrentDay() {
  let today = new Date();
  let thisMonth = today.getMonth();
  let thisYear = today.getFullYear();
  if (thisMonth === g_currentMonth && thisYear === g_currentYear) {
     return today.getDate();
  } 
}

function clearCalendarView() {
  var el = document.getElementById("calendar_id");
  if (el) {
    el.remove();
  }

  var info = document.getElementById("info_id");
  if (info) {
    info.remove();
  }
}

function createHeader() {
  let thead = document.createElement("tr");
  daysNames.forEach(day => {
    let cell = document.createElement("th");
    cell.textContent = day;
    thead.appendChild(cell);
  });
  return thead;
}

function createInfo(currentMonth, currentYear) {
  const result = document.createElement("div");
  result.textContent = (monthsNames[currentMonth]) + '  ' + currentYear;
  result.classList.add("resultMonthAndYear");
  return result;
}

function createDaysForMonth(monthName, startingDay, currentDay) {
  let tbody = document.createElement("tbody");
  let count = 1;

  let firstRow = createFirstRow(startingDay, currentDay);
  tbody.appendChild(firstRow);

  let index = daysNames.indexOf(startingDay);
  let nextMonday = 8 - index; // 7 - index + 1

  let lastMonday = createMiddleRows(tbody, nextMonday, monthName, currentDay);

  let lastRow = createLastRow(lastMonday, monthName, currentDay);
  tbody.appendChild(lastRow);

  return tbody;
}

function createFirstRow(startingDay, currentDay) {
  let row = document.createElement("tr");
  let start = daysNames.indexOf(startingDay);

  for (i = 0; i < start; i++) {
    let cell = document.createElement("td");
    row.appendChild(cell);
  }

  let count = 1;
  for (i = start; i < 7; i++) {
    let cell = document.createElement("td");
    cell.textContent = count;
    row.appendChild(cell);
    if (count === currentDay) {
      cell.classList.add("currentDay");
    }
    count++;
  }
  return row;
}

function createMiddleRows(tbody, startingDay, monthName, currentDay) {
  let monthIndex = monthsNames.indexOf(monthName);
  let daysInCurrentMonth = daysInMonth[monthIndex];
  let count = startingDay;

  while (count + 6 < daysInCurrentMonth) {
    let row = document.createElement("tr");
    for (i = 0; i < 7; i++) {
      let cell = document.createElement("td");
      cell.textContent = count;
      row.appendChild(cell);
      if (count === currentDay) {
        cell.classList.add("currentDay");
      }
      count++;
    }
    tbody.appendChild(row);
  }
  return count;
}

function createLastRow(startDay, monthName, currentDay) {
  let monthIndex = monthsNames.indexOf(monthName);
  let daysInCurrentMonth = daysInMonth[monthIndex];

  let row = document.createElement("tr");
  let count = 0;
  for (i = startDay; i <= daysInCurrentMonth; i++) {
    let cell = document.createElement("td");
    cell.textContent = i;
    row.appendChild(cell);
    if (i === currentDay) {
      cell.classList.add("currentDay");
    }
    count++;
  }
  for (i = count; i < 7; i++) {
    let cell = document.createElement("td");
    row.appendChild(cell);
  }
  return row;
}

function previous() {
  g_currentMonth--;
  if (g_currentMonth < 0) {
    g_currentMonth = 11;
    g_currentYear--;
  }
  g_startDay = (new Date(g_currentYear, g_currentMonth, 1).getDay() + 6) % 7;;
  showCalendar(g_currentMonth, g_currentYear, g_startDay);
}

function next() {
  g_currentMonth++;
  if (g_currentMonth > 11) {
    g_currentMonth = 0;
    g_currentYear++;
  }
  g_startDay = (new Date(g_currentYear, g_currentMonth, 1).getDay() + 6) % 7;
  showCalendar(g_currentMonth, g_currentYear, g_startDay);
}

//*bind execution logic with buttons
let buttonPrev = document.getElementById("previous");
buttonPrev.addEventListener("click", previous);

let buttonNext = document.getElementById("next");
buttonNext.addEventListener("click", next);

showCalendar(g_currentMonth, g_currentYear, g_startDay); //calling the function

_________________
Digital Express


Top
 Profile  
 

Re: calender javascript
PostPosted: Wed Apr 08, 2020 3:15 pm 
Offline

Joined: Tue Dec 03, 2019 1:42 pm
Posts: 2
Location: United Arab Emirates
Hi Kate, Thanks for the help but I placed this code and its work perfect for me.

Step 1: Insert the below script into the <HEAD> section of your page:

Code:
<SCRIPT LANGUAGE="JavaScript">

/*
Any-Month calendar script- Rob Patrick (rpatrick@mit.edu)
Script featured on and available at:
http://www.javascriptkit.com/
*/

function setToday() {
var now   = new Date();
var day   = now.getDate();
var month = now.getMonth();
var year  = now.getYear();
if (year < 2000)    // Y2K Fix, Isaac Powell
year = year + 1900; // http://onyx.idbsu.edu/~ipowell
this.focusDay = day;
document.calControl.month.selectedIndex = month;
document.calControl.year.value = year;
displayCalendar(month, year);
}
function isFourDigitYear(year) {
if (year.length != 4) {
alert ("Sorry, the year must be four-digits in length.");
document.calControl.year.select();
document.calControl.year.focus();
} else { return true; }
}
function selectDate() {
var year  = document.calControl.year.value;
if (isFourDigitYear(year)) {
var day   = 0;
var month = document.calControl.month.selectedIndex;
displayCalendar(month, year);
    }
}

function setPreviousYear() {
var year  = document.calControl.year.value;
if (isFourDigitYear(year)) {
var day   = 0;
var month = document.calControl.month.selectedIndex;
year--;
document.calControl.year.value = year;
displayCalendar(month, year);
   }
}
function setPreviousMonth() {
var year  = document.calControl.year.value;
if (isFourDigitYear(year)) {
var day   = 0;
var month = document.calControl.month.selectedIndex;
if (month == 0) {
month = 11;
if (year > 1000) {
year--;
document.calControl.year.value = year;
}
} else { month--; }
document.calControl.month.selectedIndex = month;
displayCalendar(month, year);
   }
}
function setNextMonth() {
var year  = document.calControl.year.value;
if (isFourDigitYear(year)) {
var day   = 0;
var month = document.calControl.month.selectedIndex;
if (month == 11) {
month = 0;
year++;
document.calControl.year.value = year;
} else { month++; }
document.calControl.month.selectedIndex = month;
displayCalendar(month, year);
   }
}
function setNextYear() {
var year = document.calControl.year.value;
if (isFourDigitYear(year)) {
var day = 0;
var month = document.calControl.month.selectedIndex;
year++;
document.calControl.year.value = year;
displayCalendar(month, year);
   }
}
function displayCalendar(month, year) {       
month = parseInt(month);
year = parseInt(year);
var i = 0;
var days = getDaysInMonth(month+1,year);
var firstOfMonth = new Date (year, month, 1);
var startingPos = firstOfMonth.getDay();
days += startingPos;
document.calButtons.calPage.value  =   " Su Mo Tu We Th Fr Sa";
document.calButtons.calPage.value += "\n --------------------";
for (i = 0; i < startingPos; i++) {
if ( i%7 == 0 ) document.calButtons.calPage.value += "\n ";
document.calButtons.calPage.value += "   ";
}
for (i = startingPos; i < days; i++) {
if ( i%7 == 0 ) document.calButtons.calPage.value += "\n ";
if (i-startingPos+1 < 10)
document.calButtons.calPage.value += "0";
document.calButtons.calPage.value += i-startingPos+1;
document.calButtons.calPage.value += " ";
}
for (i=days; i<42; i++)  {
if ( i%7 == 0 ) document.calButtons.calPage.value += "\n ";
document.calButtons.calPage.value += "   ";
}
document.calControl.Go.focus();
}
function getDaysInMonth(month,year)  {
var days;
if (month==1 || month==3 || month==5 || month==7 || month==8 || month==10 || month==12)  days=31;
else if (month==4 || month==6 || month==9 || month==11) days=30;
else if (month==2)  {
if (isLeapYear(year)) { days=29; }
else { days=28; }
}
return (days);
}
function isLeapYear (Year) {
if (((Year % 4)==0) && ((Year % 100)!=0) || ((Year % 400)==0)) {
return (true);
} else { return (false); }
}
// End -->
</SCRIPT>


Step 2: Add the below code to the <BODY> section of your page:

Code:
<big>Select-A-Month</big>
<FORM NAME="calControl" onSubmit="return false;" method="post">
<TABLE CELLPADDING=0 CELLSPACING=0 BORDER=0>
<TR><TD COLSPAN=7>
<SELECT NAME="month" onChange="selectDate()">
<OPTION>January</OPTION>
<OPTION>February</OPTION>
<OPTION>March</OPTION>
<OPTION>April</OPTION>
<OPTION>May</OPTION>
<OPTION>June</OPTION>
<OPTION>July</OPTION>

<OPTION>August</OPTION>
<OPTION>September</OPTION>
<OPTION>October</OPTION>
<OPTION>November</OPTION>
<OPTION>December</OPTION>
</SELECT>
<INPUT NAME="year" TYPE=TEXT SIZE=4 MAXLENGTH=4>
<INPUT TYPE="button" NAME="Go" value="Build!" onClick="selectDate()">
</TD>
</TR>
</FORM>
<FORM NAME="calButtons" method="post">
<TR><TD><textarea FONT="Courier" NAME="calPage" WRAP=no ROWS=8 COLS=22></textarea></TD><TR><TD>
<INPUT TYPE=BUTTON NAME="previousYear" VALUE=" <<  "  onClick="setPreviousYear()">
<INPUT TYPE=BUTTON NAME="previousYear" VALUE="  <  "  onClick="setPreviousMonth()">
<INPUT TYPE=BUTTON NAME="previousYear" VALUE="Today"  onClick="setToday()">
<INPUT TYPE=BUTTON NAME="previousYear" VALUE="  >  "  onClick="setNextMonth()">
<INPUT TYPE=BUTTON NAME="previousYear" VALUE="  >> "  onClick="setNextYear()">
</TD></TR>
</TABLE></FORM></FONT>

<p align="center">This free script provided by<br />
<a href="http://javascriptkit.com">JavaScript Kit</a></p>


Reference: https://www.digitalgravityagency.com/

_________________
Digital Gravity


Top
 Profile  
 

Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 6 posts ] 

All times are UTC + 1 hour


Who is online

Users browsing this forum: No registered users and 9 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
Jump to:  
cron
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group