469,304 Members | 2,018 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,304 developers. It's quick & easy.

Determine next Friday's date?

CK
Hi All,
Looking for a simple function to return the date for the follwing Friday. I
send out emails from time to time and I would like a function that
dynamically returns the date for the follwing Friday. So no matter what day
I send it, it will dynamically calculate Friday's date. Also if I am sending
it on a Friday, I would like to return the current date, Saturday and later
will return next Friday's date. Thanks for any advice.

Kind Regards,
CK
Feb 23 '06 #1
11 15199
CK
I wrote one that works.

function nextFriday()
{
var d = new Date();

switch (d.getDay())
{
case 0: d.setDate(d.getDate() + 5);
break;

case 1: d.setDate(d.getDate() + 4);
break;

case 2: d.setDate(d.getDate() + 3);
break;

case 3: d.setDate(d.getDate() + 2);
break;

case 4: d.setDate(d.getDate() + 1);
break;

case 6: d.setDate(d.getDate() + 6);
break;
}

return d;

}

"CK" <c_**********@hotmail.com> wrote in message
news:9x******************@newssvr21.news.prodigy.c om...
Hi All,
Looking for a simple function to return the date for the follwing Friday.
I send out emails from time to time and I would like a function that
dynamically returns the date for the follwing Friday. So no matter what
day I send it, it will dynamically calculate Friday's date. Also if I am
sending it on a Friday, I would like to return the current date, Saturday
and later will return next Friday's date. Thanks for any advice.

Kind Regards,
CK

Feb 23 '06 #2
CK wrote:
I send it, it will dynamically calculate Friday's date. Also if I am sending
it on a Friday, I would like to return the current date, Saturday and later
will return next Friday's date. Thanks for any advice.


function nextDay(day){
var d = new Date;
(day = (Math.abs(+day || 0) % 7) - d.getDay()) < 0 && (day += 7);
return day && d.setDate(d.getDate() + day), d;
};

The day parameter ranges from 0 to 6.
--
Jonas Raoni Soares Silva
http://www.jsfromhell.com
Feb 23 '06 #3
Lee wrote:
CK said:
Hi All,
Looking for a simple function to return the date for the follwing Friday. I
send out emails from time to time and I would like a function that
dynamically returns the date for the follwing Friday. So no matter what day
I send it, it will dynamically calculate Friday's date. Also if I am sending
it on a Friday, I would like to return the current date, Saturday and later
will return next Friday's date. Thanks for any advice.

var d=new Date();
d.setDate(d.getDate()+(12-d.getDay())%7);

d is now the current date if it is Friday or the date of the next Friday.


Aw, what the heck, here's another based on a for loop:

for (var x=new Date(); x.getDay()!=5; x.setDate(x.getDate()+1)){}
alert('Next Friday is ... ' + x.getDate());


--
Rob
Feb 23 '06 #4
JRS: In article <9x******************@newssvr21.news.prodigy.com >,
dated Thu, 23 Feb 2006 18:53:57 remote, seen in
news:comp.lang.javascript, CK <c_**********@hotmail.com> posted :
Looking for a simple function to return the date for the follwing Friday. I
send out emails from time to time and I would like a function that
dynamically returns the date for the follwing Friday. So no matter what day
I send it, it will dynamically calculate Friday's date. Also if I am sending
it on a Friday, I would like to return the current date, Saturday and later
will return next Friday's date. Thanks for any advice.


Before posting to a newsgroup, one should seek and read the newsgroup
FAQ; see below.

I presume that you want to use local civil dates.

Ignore any "solutions" that you may be offered that contain, implicitly
or explicitly, the number 86400000. Any such is necessarily either
incompletely reliable or unduly long.

BTW, since yours appears to be a non-Web application it may be that you
should really have asked in one of the Microsoft scripting groups, where
most questions are about local execution under WSH.

The answer is given via the FAQ, in js-date7.htm#NPXD ; with D = new
Date() :-

// X = 0..6 = Sun..Sat

D.setDate( D.getDate() + (7+X-D.getDay())%7 )

Note : Many people are sometimes still active after midnight. If you
may be such, it could be wise to subtract say four hours before
executing the above. Your personal "day" will then run from 04:00 to
28:00, local.

--
John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4
<URL:http://www.jibbering.com/faq/> JL/RC: FAQ of news:comp.lang.javascript
<URL:http://www.merlyn.demon.co.uk/js-index.htm> jscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.
Feb 23 '06 #5
RobG wrote:
[...]

Aw, what the heck, here's another based on a for loop:

for (var x=new Date(); x.getDay()!=5; x.setDate(x.getDate()+1)){}
alert('Next Friday is ... ' + x.getDate());


And if the need is for *next* Friday's date if today is Friday, then:

for (var x=new Date(); x.setDate(x.getDate()+1) && x.getDay()!=5; ){}
alert('Next Friday\'s date is: ' + x.getDate());
--
Rob
Feb 23 '06 #6
CK wrote:
I wrote one that works.

function nextFriday()
{
var d = new Date();

switch (d.getDay())
{
case 0: d.setDate(d.getDate() + 5);
break;

case 1: d.setDate(d.getDate() + 4);
break;

case 2: d.setDate(d.getDate() + 3);
break;

case 3: d.setDate(d.getDate() + 2);
break;

case 4: d.setDate(d.getDate() + 1);
break;

case 6: d.setDate(d.getDate() + 6);
break;
}

return d;

}
Date.prototype.getNextWkDay = function(iWkDay)
{
if (typeof iWkDay == "undefined"
|| typeof iWkDay != "number")
{
// Monday is the default
iWkDay = 1;
}

iWkday = Math.floor(iWkDay);

var x = iWkDay - this.getDay(), d = this;
if (x != 0)
{
var iDate = this.getDate();

d = new Date(
this.getFullYear(),
this.getMonth(),
iDate,
this.getHours(),
this.getMinutes(),
this.getSeconds(),
this.getMilliseconds());

if (x < 0)
{
x += 7;
}

d.setDate(iDate + x);
}

return d;
};

// get next Friday's date
var dNextFriday = new Date().getNextWkDay(5);
[Top post]


Don't do that again. <URL:http://jibbering.com/faq/>
PointedEars
Feb 25 '06 #7
Thomas 'PointedEars' Lahn wrote:
Date.prototype.getNextWkDay = function(iWkDay)
{
if (typeof iWkDay == "undefined"
|| typeof iWkDay != "number")
{
// Monday is the default
iWkDay = 1;
}

iWkday = Math.floor(iWkDay);

var x = iWkDay - this.getDay(), d = this;
if (x != 0)
{
var iDate = this.getDate();

d = new Date(
this.getFullYear(),
this.getMonth(),
iDate,
this.getHours(),
this.getMinutes(),
this.getSeconds(),
this.getMilliseconds());

if (x < 0)
{
x += 7;
}

d.setDate(iDate + x);
}

return d;
};

// get next Friday's date
var dNextFriday = new Date().getNextWkDay(5);


Date.prototype.getNextWkDay = function(iWkDay)
{
if (typeof iWkDay != "number" || iWkDay < 0)
{
// next Monday is the default
iWkDay = 1;
}
else
{
iWkday = Math.floor(iWkDay) % 7;
}

var x = (iWkDay - this.getDay() + 7) % 7, d = this;
if (x != 0)
{
var iDate = this.getDate();

d = new Date(
this.getFullYear(),
this.getMonth(),
iDate,
this.getHours(),
this.getMinutes(),
this.getSeconds(),
this.getMilliseconds());

d.setDate(iDate + x);
}

return d;
};

// get next Friday's date
var dNextFriday = new Date().getNextWkDay(5);
PointedEars
Feb 25 '06 #8
Thomas 'PointedEars' Lahn wrote:
Date.prototype.getNextWkDay = function(iWkDay)
{
if (typeof iWkDay == "undefined"
|| typeof iWkDay != "number")
{
// Monday is the default
iWkDay = 1;
}

iWkday = Math.floor(iWkDay);

var x = iWkDay - this.getDay(), d = this;
if (x != 0)
{
var iDate = this.getDate();

d = new Date(
this.getFullYear(),
this.getMonth(),
iDate,
this.getHours(),
this.getMinutes(),
this.getSeconds(),
this.getMilliseconds());

if (x < 0)
{
x += 7;
}

d.setDate(iDate + x);
}

return d;
};

// get next Friday's date
var dNextFriday = new Date().getNextWkDay(5);


Date.prototype.getNextWkDay = function(iWkDay)
{
if (typeof iWkDay != "number" || iWkDay < 0)
{
// next Monday is the default
iWkDay = 1;
}

iWkday = Math.floor(iWkDay) % 7;

var x = (iWkDay - this.getDay() + 7) % 7, d = this;
if (x != 0)
{
var iDate = this.getDate();

d = new Date(
this.getFullYear(),
this.getMonth(),
iDate,
this.getHours(),
this.getMinutes(),
this.getSeconds(),
this.getMilliseconds());

d.setDate(iDate + x);
}

return d;
};

// get next Friday's date
var dNextFriday = new Date().getNextWkDay(5);
PointedEars
Feb 25 '06 #9
JRS: In article <rI*****************@news.optus.net.au>, dated Thu, 23
Feb 2006 23:39:03 remote, seen in news:comp.lang.javascript, RobG
<rg***@iinet.net.au> posted :
RobG wrote:
[...]

Aw, what the heck, here's another based on a for loop:

for (var x=new Date(); x.getDay()!=5; x.setDate(x.getDate()+1)){}
alert('Next Friday is ... ' + x.getDate());


And if the need is for *next* Friday's date if today is Friday, then:

for (var x=new Date(); x.setDate(x.getDate()+1) && x.getDay()!=5; ){}
alert('Next Friday\'s date is: ' + x.getDate());


Other than
* Determining the start and end of local Summer Time
according to the OS,
* Generating a list-type result,
is there any date/time calculation possible in javascript that *needs*
iteration?

--
John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4
<URL:http://www.jibbering.com/faq/> JL/RC: FAQ of news:comp.lang.javascript
<URL:http://www.merlyn.demon.co.uk/js-index.htm> jscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.
Feb 25 '06 #10
JRS: In article <15****************@PointedEars.de>, dated Sat, 25 Feb
2006 06:11:50 remote, seen in news:comp.lang.javascript, Thomas
'PointedEars' Lahn <Po*********@web.de> posted :
var iDate = this.getDate();

d = new Date(
this.getFullYear(),
this.getMonth(),
iDate,
this.getHours(),
this.getMinutes(),
this.getSeconds(),
this.getMilliseconds());

ISTM that you are generating a new Date Object d as a clone of the Date
Object this.

Most of those get... methods require a full conversion from the stored
valueOf in milliseconds UT to Gregorian local Y M D h m s ms including
the determination of whether it is Summer (a sufficiently clever browser
may cache the first results). The constructor then has to do the
reverse.

Those who have consulted the newsgroup FAQ on the subject will prefer, I
expect, to use
d = new Date(+this)
or d = new Date(this.valueOf())

which merely copy the IEEE Double from this to d.

Since the OP wants next Friday's date, which is today-based, he can use
new Date() to get a today-object and alter its value without copying.
In the unlikely event that your iWkday is not an integer, ISTM most
likely that there has been a rounding error. Then, Math.round() will be
more appropriate than Math.floor(). ISTM that iDate will be truncated
anyway, if not integer.

Don't write bloatware; we have Merkins for that.

--
John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4
<URL:http://www.jibbering.com/faq/> JL/RC: FAQ of news:comp.lang.javascript
<URL:http://www.merlyn.demon.co.uk/js-index.htm> jscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.
Feb 25 '06 #11
Dr John Stockton wrote:
JRS: In article <rI*****************@news.optus.net.au>, dated Thu, 23
Feb 2006 23:39:03 remote, seen in news:comp.lang.javascript, RobG
<rg***@iinet.net.au> posted :
RobG wrote:
[...]
Aw, what the heck, here's another based on a for loop:

for (var x=new Date(); x.getDay()!=5; x.setDate(x.getDate()+1)){}
alert('Next Friday is ... ' + x.getDate());


And if the need is for *next* Friday's date if today is Friday, then:

for (var x=new Date(); x.setDate(x.getDate()+1) && x.getDay()!=5; ){}
alert('Next Friday\'s date is: ' + x.getDate());

Other than
* Determining the start and end of local Summer Time
according to the OS,
* Generating a list-type result,
is there any date/time calculation possible in javascript that *needs*
iteration?

I can't think of any right now. Sometimes it's interesting to look at
other ways to achieve a result, even if only to discount them for being
less appropriate.
--
Rob
Feb 26 '06 #12

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by J. Muenchbourg | last post: by
2 posts views Thread by Chris | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by harlem98 | last post: by
reply views Thread by harlem98 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.