Menu
Hi all
Is there a way to get the second Monday of the month?
I am trying to come up with a way to find the date of the queens birthday (In Australia) which is the second Monday of June, so I can have my web calender change colour to show it as a public holiday.
Does anyone have any ideas?
Thanks in advance
<i>
</i>var d, monday=0, i=1;
while(monday < 2)
{
d = new Date(i++ + " June 2005");
if(d.getDay()==1) monday++;
}
alert(d);
[code=php]function getQueensDay(year) {
var month = new Date("6/1/"+year);
var monday = month.getDate() - month.getDay() + 1;
var secondMonday = (monday < 1) ? monday + 14 : monday + 7;
return secondMonday;
}
// Try it out for the year 2005
alert(getQueensDay(2005));[/code]
[i]Originally posted by 7stud [/i]
[B]Very nice! It's much more efficient. And, it can be even better: there's no reason to call the getDate() function. ? [/B][/QUOTE]
[code=php]function getQueensDay(year) {
return ((m=2-(new Date("6/1/"+year)).getDay())<1)?m+14:m+7;
}
// Try it out for the year 2005
alert(getQueensDay(2005));[/code]
// calculate the 2nd monday of the month in june
function getSecondMondayInJune(){
var tD=0,cDays=0,curYear=new Date().getYear();
var tA=[]; // place holder array for date information
// to spimplyfy things, gather some date information,
// the test month 5 == june as javascript months are 0 to 11
for(t=1; t<18; t++){
tA[t]=new Date(curYear,5,t);
} // end of loop [t]
// now step through and check the gathered date info
for(chk=1; chk<18; chk++){
if(tA[chk].getDay()==1){ // found monday!
cDays++;
} // end of if(tA[chk])
if(cDays>=2){ // found the 2nd monday!
// return a formatted strin as d/m/yyyy
return[tA[chk].getDate(),tA[chk].getMonth()+1,tA[chk].getYear()].join("/");
} // end of if(cDays)
} // end of loop[chk]
// got here? something went seriously wrong!
return false;
}
[/QUOTE]
[i]Originally posted by senshi [/i]
[B]The thing is...
the date format as set out in the javascript reference, tells you that to stuff...
new Date(i++ + " June" + yearToday);
is not correct, the correct format that Date is expecting the date information is...
a. numeric!
b. Date(Year,Month,Date,Hour,Minute,Second)
where you can specify Date(Year,Month,Date) and this automatically sets the time to midnight or 00h 00m 00s.
You also have the issue that javascript months run 0 to 11 and not 1 to 12 so any reference to dd/06/yyyy in javascript is actually dd/07/yyyy in real world, so 1 has to be deducted from the months ref you set in the Date() call. [/B][/QUOTE]
can specify Date(Year,Month,Date) and this automatically sets the time to midnight or 00h 00m 00s.[/quote]
[CODE]
var queensBirthday = "6/" + queensBirthdayDay + "/" + yearToday;
if ((queensBirthday) == (Day_Eight)) {
document.write('<style type="text/css"><!-- #list_Day_Eight { background-color: #9E0F10; } --></style>')
} else if ((queensBirthday) == (Day_Nine)) {
document.write('<style type="text/css"><!-- #list_Day_Nine { background-color: #9E0F10; } --></style>')
} else if ((queensBirthday) == (Day_Ten)) {
document.write('<style type="text/css"><!-- #list_Day_Ten { background-color: #9E0F10; } --></style>')
} else if ((queensBirthday) == (Day_Eleven)) {
document.write('<style type="text/css"><!-- #list_Day_Eleven { background-color: #9E0F10; } --></style>')
} else if ((queensBirthday) == (Day_Twelve)) {
document.write('<style type="text/css"><!-- #list_Day_Twelve { background-color: #9E0F10; } --></style>')
} else if ((queensBirthday) == (Day_Thirteen)) {
document.write('<style type="text/css"><!-- #list_Day_Thirteen { background-color: #9E0F10; } --></style>')
} else if ((queensBirthday) == (Day_Fourteen)) {
document.write('<style type="text/css"><!-- #list_Day_Fourteen { background-color: #9E0F10; } --></style>')
}[/CODE]
[i]Originally posted by senshi [/i]
[B]you also have to remember that Javascript months do not run as you expect, so all the examples have not accounted for this so anything tou use will return 2nd monday for july and not june.[/B][/QUOTE]
[i]Originally posted by Exuro [/i]
[B][i]*grumble*[/i]
That isn't true. When you pass dates into the [FONT=courier new]Date()[/FONT] constructor as a string you write it exactly how you'd write them on a letter or see them on a calendar. The only time you have to worry about weird offsets is when you call a method to retrieve data from your constructed date, like [FONT=courier new]getMonth()[/FONT], which will return a number 0-11. However, nowhere in the function did we do anything with the month number, so we never had to do this. We did call the [FONT=courier new]getDay()[/FONT] method, but we reffered to Monday properly as 1 instead of 2, so we had no problem there either.
Sorry, but it irritates me when someone posts something misleading people into thinking what someone else said is wrong, when in fact it isn't. Had you [i]actually tested[/i] the scripts posted by me or 7stud you'd have seen that they in fact worked just fine. [/B][/QUOTE]
[i]Originally posted by senshi [/i]
[B]if you have the date, you take the value you have for a date and split it up and to return a nuimerical value, subtract zero from the string, this is the shortest and quickest method of making a numerical value as a string into a numerical value to process as a sum.[/B][/QUOTE]
[i]Originally posted by smercer [/i]
[B]that way I don't have to go the long way with this:
[CODE]
[/B][/QUOTE]
var queensBirthday = "6/" + queensBirthdayDay + "/" + yearToday;
if ((queensBirthday) == (Day_Eight)) {
document.write('<style type="text/css"><!-- #list_Day_Eight { background-color: #9E0F10; } --></style>')
} else if ((queensBirthday) == (Day_Nine)) {
document.write('<style type="text/css"><!-- #list_Day_Nine { background-color: #9E0F10; } --></style>')
}
...[/CODE]
[code=php]// 7Stud's code:
var queensBirthdayDay, monday=0, i=1;
while(monday < 2){
queensBirthdayDay = new Date(i++ + " June" + (new Date()).getFullYear());
if(queensBirthdayDay.getDay()==1) monday++;
}
queensBirthdayDay = queensBirthdayDay.getDate()
// Change the background color:
document.getElementById("list_Day_"+queensBirthdayDay).style.backgroundColor = "#9E0F10";[/code]
[code=php]// 7Stud's code:
var queensBirthdayDay, monday=0, i=1;
while(monday < 2){
queensBirthdayDay = new Date(i++ + " June" + (new Date()).getFullYear());
if(queensBirthdayDay.getDay()==1) monday++;
}
queensBirthdayDay = queensBirthdayDay.getDate()
// Change the background color:
var DAYS = ["One","Two","Three","Four","Five","Six","Seven","Eight","Nine","Ten","Eleven","Twelve","Thirteen","Fourteen"];
document.getElementById("list_Day_"+DAYS[queensBirthdayDay-1]).style.backgroundColor = "#9E0F10";[/code]
[i]Originally posted by senshi [/i]
[B]JScript
...
Direct off the MSDN[/B][/QUOTE]
[i]ECMAScript Language Specification:[/i]
15.9.3.2 new Date (value)
The [[Prototype]] property of the newly constructed object is set to the original Date prototype object,
the one that is the initial value of Date.prototype (15.9.4.1).
The [[Class]] property of the newly constructed object is set to "Date".
The [[Value]] property of the newly constructed object is set as follows:
1. Call ToPrimitive(value).
[color=red]2. If Type(Result(1)) is String, then go to step 5.[/color]
3. Let V be ToNumber(Result(1)).
4. Set the [[Value]] property of the newly constructed object to TimeClip(V) and return.
[color=red]5. Parse Result(1) as a date, in exactly the same manner as for the parse method (15.9.4.2); let V [/quote]
be the time value for this date.
6. Go to step 4.[/color]
If you can't do that, then you could always type out all the words for 1-14 and reference them that way:
PHP:
--------------------------------------------------------------------------------
// 7Stud's code:
var queensBirthdayDay, monday=0, i=1;
while(monday < 2){
queensBirthdayDay = new Date(i++ + " June" + (new Date()).getFullYear());
if(queensBirthdayDay.getDay()==1) monday++;
}
queensBirthdayDay = queensBirthdayDay.getDate()
// Change the background color:
var DAYS = ["One","Two","Three","Four","Five","Six","Seven","Eight","Nine","Ten","Eleven","Twelve","Thirteen","Fourteen"];
document.getElementById("list_Day_"+DAYS[queensBirthdayDay-1]).style.backgroundColor = "#9E0F10";[/quote]
[b]dateObj = new Date(dateVal)
dateVal
Required. If a numeric value, dateVal represents the number of milliseconds in Universal Coordinated Time between the specified date and midnight January 1, 1970.[color="red"]If a string, dateVal is parsed according to the rules in the parse method.[/color] [/b][/quote]
Date.parse (string)
The parse function applies the ToString operator to its argument and interprets the resulting string as a date; it returns a number, the UTC time value corresponding to the date. The string may be interpreted as a local time, a UTC time, or a time in some other time zone, depending on the contents of the string.[/quote]
The following rules govern what the parse method can successfully parse:
* Short dates can use either a "/" or "-" date separator, but must follow the month/day/year format, for example "7/20/96".
* Long dates of the form "July 10 1995" can be given with the year, month, and day in any order, and the year in 2-digit or 4-digit form. If you use the 2-digit form, the year must be greater than or equal to 70.
* Any text inside parentheses is treated as a comment. These parentheses may be nested.
* Both commas and spaces are treated as delimiters. Multiple delimiters are permitted.
* Month and day names must have two or more characters. Two character names that are not unique are resolved as the last match. For example, "Ju" is resolved as July, not June.
* The stated day of the week is ignored if it is incorrect given the remainder of the supplied date. For example, "Tuesday November 9 1996" is accepted and parsed even though that date actually falls on a Friday. The resulting Date object contains "Friday November 9 1996".
* JScript handles all standard time zones, as well as Universal Coordinated Time (UTC) and Greenwich Mean Time (GMT).
* Hours, minutes, and seconds are separated by colons, although all need not be specified. "10:", "10:11", and "10:11:12" are all valid.
* If the 24-hour clock is used, it is an error to specify "PM" for times later than 12 noon. For example, "23:15 PM" is an error.
* A string containing an invalid date is an error. For example, a string containing two years or two months is an error.[/quote]
Are there cross browser issues? I've tested the format "1 Jan 2005" in IE6 and FF1.0, and it works in both.
[i]Originally posted by Exuro [/i]
[b]
Is there any way you could change the IDs of the calendar boxes? Instead of having them like [FONT=courier new]#list_Day_Nine[/FONT] could you change it to [FONT=courier new]#list_Day_9[/FONT]? This way you could greatly simplify your code to something like this:
[code=php]// 7Stud's code:
var queensBirthdayDay, monday=0, i=1;
while(monday < 2){
queensBirthdayDay = new Date(i++ + " June" + (new Date()).getFullYear());
if(queensBirthdayDay.getDay()==1) monday++;
}
queensBirthdayDay = queensBirthdayDay.getDate()
// Change the background color:
document.getElementById("list_Day_"+queensBirthdayDay).style.backgroundColor = "#9E0F10";[/code]
[/B][/QUOTE]
[i]Originally posted by senshi [/i]
[B]what you refer to is not javascript but ActiveX which is used to parse the string and attempt to recover a meaningful date, the other method, is quite clear and has no margin for error. [/B][/QUOTE]
[i]Originally posted by Exuro [/i]
[B]That [i]really[/i] didn't make any sense to me... But, anyway!
Is there any way you could change the IDs of the calendar boxes? Instead of having them like [FONT=courier new]#list_Day_Nine[/FONT] could you change it to [FONT=courier new]#list_Day_9[/FONT]? This way you could greatly simplify your code to something like this:
[code=php]// 7Stud's code:
var queensBirthdayDay, monday=0, i=1;
while(monday < 2){
queensBirthdayDay = new Date(i++ + " June" + (new Date()).getFullYear());
if(queensBirthdayDay.getDay()==1) monday++;
}
queensBirthdayDay = queensBirthdayDay.getDate()
// Change the background color:
document.getElementById("list_Day_"+queensBirthdayDay).style.backgroundColor = "#9E0F10";[/code]
If you can't do that, then you could always type out all the words for 1-14 and reference them that way:
[code=php]// 7Stud's code:
[/B][/QUOTE]
var queensBirthdayDay, monday=0, i=1;
while(monday < 2){
queensBirthdayDay = new Date(i++ + " June" + (new Date()).getFullYear());
if(queensBirthdayDay.getDay()==1) monday++;
}
queensBirthdayDay = queensBirthdayDay.getDate()
// Change the background color:
var DAYS = ["One","Two","Three","Four","Five","Six","Seven","Eight","Nine","Ten","Eleven","Twelve","Thirteen","Fourteen"];
document.getElementById("list_Day_"+DAYS[queensBirthdayDay-1]).style.backgroundColor = "#9E0F10";[/code]
[i]Originally posted by smercer [/i]
[B]I was not aware that you could use numbers in a varable name especally with JavaScript, which is why I have done it like this. [/B][/QUOTE]
[i]Originally posted by smercer [/i]
[B]This is getting complicated, but thanks for the big help.
Exuro: I would like to know more about using Arrays, the books I have explain very little about it, which is where I got the first Array in the script I am posting.[/B][/QUOTE]
0.1.9 — BETA 5.28