Determine next Friday's date?

C

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
 
L

Lee

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.

getDay() returns the number of days since the last Sunday,
so 5-getDay() is the number of days to the closest Friday.
Adding 7 to that value and taking it mod 7 translates it to
the number of days until the next Friday (zero if today is
Friday). Add that number of days to the current date to get
the date of the next Friday.
 
C

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;

}
 
J

Jonas Raoni

CK said:
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.
 
D

Dr John Stockton

JRS: In article <[email protected]>,
dated Thu, 23 Feb 2006 18:53:57 remote, seen in
news:comp.lang.javascript said:
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.
 
R

RobG

Lee said:
CK said:



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());
 
R

RobG

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());
 
D

Dr John Stockton

JRS: In article <[email protected]>, dated Thu, 23
Feb 2006 23:39:03 remote, seen in RobG
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?
 
T

Thomas 'PointedEars' Lahn

CK said:
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
 
T

Thomas 'PointedEars' Lahn

Thomas said:
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
 
T

Thomas 'PointedEars' Lahn

Thomas said:
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
 
D

Dr John Stockton

JRS: In article <[email protected]>, dated Sat, 25 Feb
2006 06:11:50 remote, seen in Thomas
'PointedEars' Lahn said:
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.
 
R

RobG

Dr said:
JRS: In article <[email protected]>, dated Thu, 23
Feb 2006 23:39:03 remote, seen in RobG
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.
 

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

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,011
Latest member
AjaUqq1950

Latest Threads

Top