473,230 Members | 1,434 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,230 software developers and data experts.

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 15864
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: J. Muenchbourg | last post by:
Looking thru my resources, I cannot find a simple way of displaying next sunday's date depending on what todays date is. I'd like to do it without doing 7 if statements or case select: ...
7
by: Adrian | last post by:
I hit on this problem converting a VB.NET insurance application to C#. Age next birthday calculated from date of birth is often needed in insurance premium calculations. Originally done using...
5
by: vj | last post by:
I'm doing: a = now() delta = ReltaiveDateTime(days=+6, weekday(mx.DateTime.Friday, 0)) Next Friday: a+delta a: march 23 a+delta: Gives me March 31st and not March 24th Any ideas?
6
by: Kentor | last post by:
does anyone know of a script that would produce a calendar starting from todays date with checkboxes next to every day of the month so that the user is able to check it in case of an event for that...
11
by: shankindc | last post by:
Hi, I have the following requirements In a form, there exists a date field which needs to be automatically populated. The date has to be the Friday of every week. Assume in a calendar week Feb...
1
by: Matt | last post by:
I have table that has the following fields: JobNumber WeekEndingDate ReportRequired_01 ReportRequired_02 every Friday morning the weekending date changes. I need to automatically generate...
2
by: Chris | last post by:
I need to find a way to write a query that will return the date of the Friday of four weeks ago. So, if I use today's date (7/30), I should be returning 7/6 as 4 weeks ago on Friday. Any help is...
34
by: -Lost | last post by:
I'm REALLY liking this so far. And for those who welcome something a little less cryptic than what the resident date guru offers, here's a chance to try something fairly elegant and definitely...
2
by: ncsthbell | last post by:
I am having problems getting the end date to calculate correctly. I start with Quarter '03/02', (YY/QTR), for this it means it is for the 2nd qtr of 2003. My goal is to get the begin & end dates...
0
by: VivesProcSPL | last post by:
Obviously, one of the original purposes of SQL is to make data query processing easy. The language uses many English-like terms and syntax in an effort to make it easy to learn, particularly for...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.