...

View Full Version : get date of Sunday



boeing747fp
06-21-2004, 03:59 PM
i need a code that will get the "this coming Sunday" date - like if it is Tuesday, it will find the date of the upcoming Sunday, but yet if it is Sunday, display the current date?

homerUK
06-21-2004, 04:26 PM
you could do a switch on the current day, say if it's tuesday, you know that there are 5 days until sunday (inc tuesday)... so get the current date and add 5 to the days..... not got chance to code it at the mo - but that principle should work!?

jalarie
06-21-2004, 05:02 PM
How's this:

Now=new Date();
Now_Y=Now.getYear();
Now_M=Now.getMonth();
Now_D=Now.getDate();
Now_W=Now.getDay();
if (Now_Y < 70) { Now_Y=Now_Y*1+2000; }
if (Now_Y < 1900) { Now_Y=Now_Y*1+1900; }
if (Now_W != 0) { Now_D=Now_D*1+7-Now_W; }
Then=new Date(Now_Y,Now_M,Now_D);
alert(Then);

Willy Duitt
06-21-2004, 05:37 PM
Edit: Removed content per.........

nolachrymose
06-21-2004, 05:46 PM
How about this?


Date.prototype.getNextSunday = function() {
var nextSunday = this;
switch(nextSunday.getDay()) {
case 0: return nextSunday; //it's Sunday!
default: nextSunday.setDate(nextSunday.getDate() + (7 - nextSunday.getDay()));
return nextSunday;
}
return false;
}

Hope that helps!

Happy coding! :)

boeing747fp
06-21-2004, 06:20 PM
I orginally wrote it for gestation periods but it can be used for weekly chat countdowns and in your case, the mention of sunday makes me think of Church Services.

-actually... that's exactly what it's for... im setting up a church MultiMedia website at http://media.bangorbaptist.org/

Willy Duitt
06-21-2004, 06:40 PM
If you want to use it to display the time of the next reoccuring scheduled Service or Bible Study the time display I know would need some work. Also, if you would like to use it to display countdowns for several different meetings/events with a little work the script could be adapted to be more object oriented and be reused multiple times.

jalarie
06-21-2004, 06:46 PM
im setting up a church MultiMedia website at http://media.bangorbaptist.org/

You need to add a PICS label so that Parental-Controls don't block you as a porn site.

boeing747fp
06-21-2004, 06:59 PM
nah.. it's only for members of our media crew

boeing747fp
06-21-2004, 07:31 PM
none of those codes output anything onto the page

Willy Duitt
06-22-2004, 02:25 AM
none of those codes output anything onto the page

Hmmm;

Ok, on behalf of everyone whom tried to help you.....

Good Luck....

boeing747fp
06-22-2004, 03:20 AM
all i need is a script that shows the date of the upcoming sunday and if it is sunday, show the current date... but i tried the scripts above and nothing is outputted

Willy Duitt
06-22-2004, 03:39 AM
all i need is a script that shows the date of the upcoming sunday and if it is sunday, show the current date... but i tried the scripts above and nothing is outputted

Oh;

You are looking for a script which utterly and completely fits your particular needs without editting or modification upon your part. In that case try posting in the Work Offers/Requests (http://www.codingforums.com/forumdisplay.php?f=36) section of this forum and perhaps you can find someone willing to write a script to your specifications.

Good Luck;
God Bless....

glenngv
06-22-2004, 04:07 AM
all i need is a script that shows the date of the upcoming sunday and if it is sunday, show the current date... but i tried the scripts above and nothing is outputted
Have you tried nolachrymose's suggestion?

This is how to use his script:

var today = new Date();
alert(today.getNextSunday());

That will alert the date including the time. If you just want to display the date, you need to do it like this:

var today = new Date();
var sun = today.getNextSunday();
alert(sun.getMonth()+1 + "/" + sun.getDate() + "/" + sun.getFullYear())

Take note that I used alert for demo purposes only.

boeing747fp
06-22-2004, 04:19 PM
thanks


<script>
Date.prototype.getNextSunday = function() {
var nextSunday = this;
switch(nextSunday.getDay()) {
case 0: return nextSunday; //it's Sunday!
default: nextSunday.setDate(nextSunday.getDate() + (7 - nextSunday.getDay()));
return nextSunday;
}
return false;
}
var today = new Date();
var sun = today.getNextSunday();
document.write(sun.getMonth()+1 + "/" + sun.getDate() + "/" + sun.getFullYear());
</script>

that worked.... thanks!

glenngv
06-23-2004, 03:55 AM
But I hate to see that return false because the function's return type is inconsistent eventhough it will never be executed at all.

You can simplify the code like this:


Date.prototype.getNextSunday = function() {
var nextSunday = this;
var d = nextSunday.getDay();
if (d>0) nextSunday.setDate(nextSunday.getDate() + (7 - d));
return nextSunday;
}

nolachrymose
06-23-2004, 07:11 PM
Heh, I know it'll never be executed. I just put it there out of habit -- sorry. Fortunately, data return types don't *have* to be consistent in JavaScript. :p

glenngv
06-24-2004, 04:29 AM
But in a more logical sense, I think my modification is better and it could be further simplified to just one line. ;)


Date.prototype.getNextSunday = function() {
return (this.getDay()>0) ? new Date(this.setDate(this.getDate() + (7-this.getDay()))) : this;
}



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum