473,404 Members | 2,195 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,404 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 15915
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
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.