Getting the date of 4 Sundays before 25 Dec (Christmas)

A

Andrew Kooi

Greetings,

I am trying to look for a script which will give me the date of 4
Sundays before Christmas.

For example, for 2004, the date for 4 Sundays before Christmas is 28
Nov 2004. With this date, I would like to display it as part of the
text on my website.

Any ideas or assistance on this is much appreciated.

Regards,

Andrew
 
L

Lasse Reichstein Nielsen

I am trying to look for a script which will give me the date of 4
Sundays before Christmas.
For example, for 2004, the date for 4 Sundays before Christmas is 28
Nov 2004. With this date, I would like to display it as part of the
text on my website.

Any ideas or assistance on this is much appreciated.

First, find the date for next Christmas eve (the last potential sunday
before the 25th), then we find the last sunday before or on that date,
and then we backtrack a week three times:
---
var today = new Date();
var xmas = new Date(today.getFullYear(),11,24); // 24th of December this year

if (xmas < today) { // next christmas is next year
xmas.setFullYear(xmas.getFullYear()+1);
}

// Then find the previous sunday:
var sunday = new Date(xmas.getFullYear(),
xmas.getMonth(),
xmas.getDate()-xmas.getDay());

var sundays = [,,,sunday];

for(var i=2;i>=0;i--) {
var tmp = sundays[i+1];
sundays = new Date(tmp.getFullYear(),tmp.getMonth(),tmp.getDate()-7);
}
 
R

Richard Cornford

cp said:
var sundays = [,,,sunday];

Forgive a poor Perl programmer, but what does this
notation mean?

It is an Array literal defining a 4 element array in which the first 3
elements are empty (undefined) and the final element has the value held
in the variable with the identifier - sunday -, a reference to a Date
object.

Richard.
 
L

Lasse Reichstein Nielsen

cp said:
Lasse Reichstein Nielsen said:
var sundays = [,,,sunday];

Forgive a poor Perl programmer, but what does this notation mean?

Create a local variable called "sundays". Initialize it to a valua that is
an array with length 4 and the value of the variable "sunday" as its
fourth element (index 3).

That means that after this line
sundays.length == 4
sundays[3] == sunday
and (if the browser follows the ECMAScript standard) there are no
properties named 0, 1, and 2.

It is equivalent to
var sundays = [];
(or
var sundays = new Array();
} followed by
sundays[3] = sunday;


/L
 
D

Dr John Stockton

JRS: In article <[email protected]>, seen in
Lasse Reichstein Nielsen <[email protected]>
posted at Fri, 30 Jan 2004 10:26:57 :-

The following can be simplified :
var today = new Date();
var xmas = new Date(today.getFullYear(),11,24); // 24th of December this year

I do NOT like the conjunction of that variable name & that comment!
it's liable to have the "obvious typo" corrected !!!
if (xmas < today) { // next christmas is next year
xmas.setFullYear(xmas.getFullYear()+1);
}

// Then find the previous sunday:
var sunday = new Date(xmas.getFullYear(),
xmas.getMonth(),
xmas.getDate()-xmas.getDay());

var sundays = [,,,sunday];

for(var i=2;i>=0;i--) {
var tmp = sundays[i+1];
sundays = new Date(tmp.getFullYear(),tmp.getMonth(),tmp.getDate()-7);
}



var today = new Date();
var X = new Date(today.setMonth(11, 24)); // 24th of December this year

if (X < today) X.setFullYear(X.getFullYear()+1) // next Xmas next year

X.setDate(X.getDate()-X.getDay()+7) // The next Sunday

var sundays = [];

for (var i=3;i>=0;i--) { X.setDate(X.getDate()-7) ; sundays = X }

It's the same algorithm, but the loop has an extra step which slightly
reduces code.


Actually, the OP only wants the 4th Sunday, not 4 Sundays; so it is
sufficient to go back from 4 weeks earlier - replace 24 by -4 - apart
from the comparison with today - unless he wants the next 4th Sun before
Xmas rather than the 4th Sunday before next Xmas - so change +7 to -21.


var today = new Date();
var X = new Date(today.setMonth(11, 24)); // 24th of December this year

if (X < today) X.setFullYear(X.getFullYear()+1) // next Xmas next year

X.setDate(X.getDate()-X.getDay()-21) // The Sunday



In each, X.setHours(0,0,0,0) can be added if needed.


Actually, the OP perhaps wants Advent Sunday, the Sunday nearest to St
Andrew's Day, Nov 30 -
see <URL:http://www.merlyn.demon.co.uk/holydays.htm>
not <URL:http://www.merlyn.demon.co.uk/holidays.htm>

in which I use

E = new Date(Y, 10, 27)
E.setDate(E.getDate() + (7-E.getDay())%7)

to set E to Advent Sunday; but there, the year is already known.
 
L

Lasse Reichstein Nielsen

Dr John Stockton said:
JRS: In article <[email protected]>, seen in
Lasse Reichstein Nielsen <[email protected]>
posted at Fri, 30 Jan 2004 10:26:57 :-

The following can be simplified :


I do NOT like the conjunction of that variable name & that comment!
it's liable to have the "obvious typo" corrected !!!

Not so obvious again. To me, christmas *is* the 24th of december,
not the 25th as in, e.g., England (if that is the obvious typo).
I should ofcourse not make comments in English then :)
X.setDate(X.getDate()-X.getDay()+7) // The next Sunday
var sundays = [];
for (var i=3;i>=0;i--) { X.setDate(X.getDate()-7) ; sundays = X }

It's the same algorithm, but the loop has an extra step which slightly
reduces code.


But it won't work, because you assign the same value (the same object)
to all the entries of the array. It will en up holding four references
to the same (earliest) date. Change the last assignment to:
sundays=new Date(X);

/L
 
A

Andrew Kooi

Greetings,

I will be trying out the code you suggested. Just wondering how I am
to display the result in between text on my homepage?

Thanx for your help.


Regards,

Andrew

Lasse Reichstein Nielsen said:
Dr John Stockton said:
JRS: In article <[email protected]>, seen in
Lasse Reichstein Nielsen <[email protected]>
posted at Fri, 30 Jan 2004 10:26:57 :-

The following can be simplified :


I do NOT like the conjunction of that variable name & that comment!
it's liable to have the "obvious typo" corrected !!!

Not so obvious again. To me, christmas *is* the 24th of december,
not the 25th as in, e.g., England (if that is the obvious typo).
I should ofcourse not make comments in English then :)
X.setDate(X.getDate()-X.getDay()+7) // The next Sunday
var sundays = [];
for (var i=3;i>=0;i--) { X.setDate(X.getDate()-7) ; sundays = X }

It's the same algorithm, but the loop has an extra step which slightly
reduces code.


But it won't work, because you assign the same value (the same object)
to all the entries of the array. It will en up holding four references
to the same (earliest) date. Change the last assignment to:
sundays=new Date(X);

/L
 
D

Dr John Stockton

JRS: In article <[email protected]>, seen in
news:comp.lang.javascript said:
var sundays = [];
for (var i=3;i>=0;i--) { X.setDate(X.getDate()-7) ; sundays = X }

It's the same algorithm, but the loop has an extra step which slightly
reduces code.


But it won't work, because you assign the same value (the same object)
to all the entries of the array. It will en up holding four references
to the same (earliest) date. Change the last assignment to:
sundays=new Date(X);



Agreed. My testing method made that result insufficiently visible.

This (which lists in the reverse order) has the insignificant advantage
of not leaving a second disused Date Object behind at the end; and
reinstates the correct next-year part (I'm not convinced that the
original question calls for it).


var today = new Date(2004,11,24)
var X = new Date(today.getFullYear(),11,24); // 24th December this year

if (X < today) X.setFullYear(X.getFullYear()+1) // next Xmas next year

var Y = X.getFullYear()

X = X.getDate()-X.getDay() // The date of the 1st Sunday before Dec 25

var Sundays = []

for (var i=0;i<3;i++) Sundays = new Date(Y, 11, X-7*i)



OP: To display Advent Sunday amidst text -



In HTML, I write "Advent Sunday is

<script type="text/javascript">

function LZ(x) { return (x<0||x>=10?"":"0") + x }

var E = new Date()
E.setMonth(11, 3) // St Andrew + 3
E.setDate(3 - E.getDay()) // - 0..6
document.write(
E.getFullYear()+'-'+LZ(E.getMonth()+1)+'-'+LZ(E.getDate()) )
</script>

this year" back in HTML.



- to write it after the page is shown, see FAQ 4.15.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,444
Messages
2,571,709
Members
48,796
Latest member
Greg L.
Top