Date element date enabling last thursdayin calendar

jbadiag

Member
Hi, I wrote this script in javascript for element date to enable only last thursday of the month, but I don't know what I'm doing wrong:

JavaScript:
var dia = date.getDate();
var currentTime = new Date(); 
var currentDay = currentTime.getDay(); 
var dayOfWeek = 4; // 0 = Sun, 3 = Wed, 6 = Sat 
var daysBack, pastDate, djous; 
if (currentDay > dayOfWeek) { 
  daysBack = currentDay - dayOfWeek; 
} 
else { 
  daysBack = (7 - dayOfWeek) + currentDay; 
} 
// Convert days back to milliseconds 
// days * num hours in a day * num mins in an hour * num sec in a min * num ms in a sec 
daysBack = daysBack * 24 * 60 * 60 * 1000; 
pastDate = new Date(currentTime.getTime() - daysBack);
dijous = pastDate.getDate();

if (dia == dijous){
  result = false; }
  else {
    result = true;
  }
It only shows last thursday but in the next month the result is not the same. Can I use any other var like date in the calendar? any solution?
Thanks
 
Try ...

Code:
var lastThursday = new Date(date);
lastThursday.setDate(lastThursday.getLastDayOfMonth());
while (lastThursday.getDay() != 4) {
   lastThursday.setDate(lastThursday.getDate() - 1);
}
var result = date.getDate() != lastThursday.getDate();

What that should do is set lastThursday to be the same as 'date' (the day being tested). Then set that to the last day of the month. Then loop around taking one day off it, until the day of the week is 4. That gives us the last Thursday. Then test that against the day of the month of 'date'.

That's off the top of my head, haven't tested it, but should work.

-- hugh
 
Did this work? I'm interested in knowing if that code works, as I'll probably put it in the wiki as an example if it does.

-- hugh
 
As punishment for your tardiness, would you mind writing it up in the Wiki section for the date element? I think it's a pretty good example. Not a biggie if you don't feel like it, but I'd appreciate it.

Hugh


Sent from my Nexus 7 using Tapatalk
 
Yes, of course i'll do it now, but to do it more difficult, can we do the same with the second thursday (and the last as well, like now)?
Please tell me how to write it in the wiki
Thanks Cheesegrits
 
I think this should do it ...

Code:
var result = true;

var secondThursday = new Date(date);
thursdays=0; 
secondThursday.setDate(1); 
while (thursdays <2) {
   secondThursday.setDate(secondThursday.getDate() + 1);
   if (secondThursday.getDay() == 4) {
      thursdays++;
   }
}

if (date.getDate() === secondThursday.getDate()) {
   result = false;
}
else {
   var lastThursday = new Date(date);
   lastThursday.setDate(lastThursday.getLastDayOfMonth());
   while (lastThursday.getDay() != 4) {
      lastThursday.setDate(lastThursday.getDate() - 1);
   }
   if (date.getDate() === lastThursday.getDate()) {
      result = false;
   }
}

Which does a similar test to see if date is the second Thursday. If it is, set result to false (so don't disable), and don't bother working out the last Thursday. If it's not the second Thursday, go ahead and test to see if it's the first Thursday. If it's neither, result will be true (set in the first line and not changed) so date gets disabled.

Haven't tested it.

-- hugh
 
Hi again, after installing last version of fabrik 3.4.1 in joomla 3.4.8 this javascript code does not work
Tried to see what happen with firebug and I give an error. Attaching image.
Please help
 

Attachments

  • Captura.JPG
    Captura.JPG
    59.1 KB · Views: 122
Hmmm, strange. That's part of the advanced date format JS, which is a 3rd party lib. No idea why or how the minified JS would break in our release, as we don't minify it.

Anyway, I went ahead and updated the whole datejs lib, which also added language handling for it (so it'll now support reverse formatting "lundi 23 avril", etc).

You'll need to update from github to get the fix.

-- hugh
 
We are in need of some funding.
More details.

Thank you.

Members online

Back
Top