PDA

View Full Version : how to display only tuesday date using date function



meenakshi
05-23-2008, 08:59 AM
hi forum
i have tried by using getDay() to get the week number but what next?i m blank
i mean i want to show 27th may 2008 this week till monday as the coming tuesday and when it is tuesday that is 27th it should show next tuesday is 3rd june 2008 till next monday and so on
any idea how should i do that:)
pls help
thanks
anand

meenakshi
05-23-2008, 10:15 AM
anyone can help me plss?

coothead
05-23-2008, 11:07 AM
Hi there meenakshi,

have a look at this example, it may suit your requirements...


<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>future planning</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">

<style type="text/css">
#info {
width:20em;
line-height:2em;
border:0.2em double #33c;
font-family:verdana,arial,helvetica,sans-serif;
font-size:1em;
color:#006;
text-align:center;
background-color:#ccf;
margin:1.6em auto;
}
</style>

<script type="text/javascript">

window.onload=function() {

/*************************************************************

start will generate the day of the week.
So 1 will set it for Monday and 7 will set it for Sunday.

*************************************************************/

now=new Date();
yy=now.getUTCFullYear();
mm=now.getMonth();
dd=now.getDate();
start=new Date(2007,0,2); /* this will produce a Tuesday date add ifinitum */
days=Math.floor((now-start)/(1000*60*60*24));

dd=dd+(7-(days % 7)); /* dd-(days % 7);would be this weeks Tuesday */

next=new Date(yy,mm,dd).toLocaleDateString().replace('Tuesday,','');

document.getElementById('info').firstChild.nodeValue='Next Tuesday is the '+next+'.';
}

</script>

</head>
<body>

<div id="info">&nbsp;</div>

</body>
</html>

coothead

jscheuer1
05-23-2008, 11:47 AM
coothead,

You forgot to declare your variables. Your use of the toLocaleDateString method will fail in many languages, and should be formatted slightly differently even for US English. Instead of using toLocaleDateString, the mm values (in this case) should be converted directly to the desired string values. That would also have the effect of dropping 'Tuesday' or 'Tue' and their equivalents in other tongues, no matter what the language on the user's computer.

meenakshi
05-23-2008, 11:51 AM
hi
wow
it worked
thanks a tonne
:)
smile always
anand

jscheuer1
05-23-2008, 12:04 PM
It may work for you now, but how about someone in China, or Mexico? Or how about when you have other scripts or objects on your page that might conflict with the undeclared globals in the code? This is much better:


<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>future planning</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">

<style type="text/css">
#info {
width:20em;
line-height:2em;
border:0.2em double #33c;
font-family:verdana,arial,helvetica,sans-serif;
font-size:1em;
color:#006;
text-align:center;
background-color:#ccf;
margin:1.6em auto;
}
</style>

<script type="text/javascript">

window.onload=function() {

/*************************************************************

start will generate the day of the week.
So 1 will set it for Monday and 7 will set it for Sunday.

*************************************************************/
var
now=new Date(),
yy=now.getFullYear(),
mm=now.getMonth(),
dd=now.getDate(),
start=new Date(2007,0,2), /* this will produce a Tuesday date add ifinitum */
days=Math.floor((now-start)/(1000*60*60*24)),
months=['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'];

dd=dd+(7-(days % 7)); /* dd-(days % 7);would be this weeks Tuesday */

var next=months[mm] +' '+ dd +', '+ yy;

document.getElementById('info').firstChild.nodeValue='Next Tuesday is '+next+'.';
}

</script>

</head>
<body>
<div id="info">&nbsp;</div>
</body></html>

meenakshi
05-23-2008, 12:54 PM
wow
thanks
:)
i will take care of it for sure:)
smile always
anand

meenakshi
05-27-2008, 08:08 AM
hi there is a little problem with the above code
what changes i have to do pls let me know
now today is 27.05.2008 and tuesday and it should show the next tuesday that is 3rd june 2008 but the code is showing the date as 34 may 2008
pls help what changes have to be done


var
now=new Date(),
yy=now.getFullYear(),
mm=now.getMonth(),
dd=now.getDate(),
start=new Date(2007,0,2), /* this will produce a Tuesday date add ifinitum */
days=Math.floor((now-start)/(1000*60*60*24)),
months=['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'];

dd=dd+(7-(days % 7)); /* dd-(days % 7);would be this weeks Tuesday */

var next=months[mm] +' '+ dd +', '+ yy;

document.getElementById('info').firstChild.nodeValue='Next Tuesday is '+next+'.';
}


smile always
anand:)

jscheuer1
05-27-2008, 08:56 AM
<script type="text/javascript">

window.onload=function() {

/*************************************************************

start will generate the day of the week.
So 1 will set it for Monday and 7 will set it for Sunday.

*************************************************************/
var
now=new Date(),
yy=now.getFullYear(),
mm=now.getMonth(),
dd=now.getDate(),
start=new Date(2007,0,2), /* this will produce a Tuesday date add ifinitum */
days=Math.floor((now-start)/(1000*60*60*24)),
months=['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'];

dd=dd+(7-(days % 7)); /* dd-(days % 7);would be this weeks Tuesday */
var nextTue=now.setDate(dd);
yy=now.getFullYear();
mm=now.getMonth();
dd=now.getDate();
var next=months[mm] +' '+ dd +', '+ yy;

document.getElementById('info').firstChild.nodeValue='Next Tuesday is '+next+'.';
}

</script>

coothead
05-27-2008, 08:58 AM
Hi there meenakshi,

unlike John, I am only an amateur coder, and do this stuff mainly as a mental stimulant in the hope that it may help stave off encroaching senile dementia.

As John has pointed out my offering to you may not work in Mexico or China and is littered with undesirable undeclared globals.
Nevertheless, as you have discovered, his code displays...

Next Tuesday is May 34, 2008. :eek:
...which, I imagine, is not really satisfactory to your requirements. :(
My code, whilst being infected with a multitude of unacceptable coding displays...

Next Tuesday is the 03 June 2008. ;)
Why this happens, I do not know but I am sure that John will be better able to explain. :)

coothead

meenakshi
05-27-2008, 09:08 AM
hi john
thanks for the reply
yes it is working fine now:)
thankse once again:)
smile always
anand

jscheuer1
05-27-2008, 10:31 AM
Why this happens, I do not know but I am sure that John will be better able to explain. :)

It happens because, for all its multitude of problems, the toLocaleDateString() method treats the date in the same way that the Date object itself would. Both correct for dates out of range by adding or subtracting months as required. My code (from my previous post in this thread) handles that by running the date through the Date object after it has been incremented and before parsing its months, etc. Here is a more condensed version which includes date suffixes like 3rd and 4th:


<script type="text/javascript">

window.onload=function() {

/*************************************************************

start will generate the day of the week.
So 1 will set it for Monday and 7 will set it for Sunday.

*************************************************************/
var
now=new Date(),
yy, mm, dd=now.getDate(),
start=new Date(2007,0,2), /* this will produce a Tuesday date add ifinitum */
days=Math.floor((now-start)/(1000*60*60*24)),
months=['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'],
sfx={1:'st', 2:'nd', 3:'rd', 21:'st', 22:'nd', 23:'rd', 31:'st'};

dd=dd+(7-(days % 7)); /* dd-(days % 7);would be this weeks Tuesday */
now.setDate(dd);
yy=now.getFullYear();
mm=now.getMonth();
dd=now.getDate();
sfx=sfx[dd]? sfx[dd] : 'th';

document.getElementById('info').firstChild.nodeValue = 'Next Tuesday is ' + months[mm] + ' ' + dd + sfx + ' ' + yy;
}

</script>

I have to give coothead credit for coming up with the method:

start=new Date(2007,0,2), /* this will produce a Tuesday date */

and the associated math that goes with it. I'm not sure it will be accurate in all cases, but I can't see why not at the moment. And it certainly is clever.

jscheuer1
05-27-2008, 11:27 AM
Here's another version that uses the day number to find the next Tue, The math is simpler, but should be as, if not more accurate than using the first Tue in 2007 as a reference:


<script type="text/javascript">

window.onload=function() {

/*****************
Next Tuesday
*****************/

/* declare our variables - using 2AM should avoid any unpleasantness from DST */
var oDate=new Date(); oDate.setHours(2); var yy, mm, day=oDate.getDay(), dd=oDate.getDate()-day+2,
/* day will be how far we are from the previous Sunday, subtracting it from the date and adding 2 will give us Tue */
months=['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'],
sfx={1:'st', 2:'nd', 3:'rd', 21:'st', 22:'nd', 23:'rd', 31:'st'};

dd=day<2? dd : dd+7; /* if we are prior to Tue, we have it, else adding 7 = next Tue */
oDate.setDate(dd); /* pass our new date back to the oDate Date object for any needed changes in date/month/year figures */
yy=oDate.getFullYear();
mm=oDate.getMonth();
dd=oDate.getDate();
sfx=sfx[dd]? sfx[dd] : 'th';

document.getElementById('info').firstChild.nodeValue = 'Next Tuesday is ' + months[mm] + ' ' + dd + sfx + ' ' + yy;
}

</script>

meenakshi
05-28-2008, 06:40 AM
hi john n coothead
you both guys are like gems
:)
it wa such a nice learning experience seeing u giving options and the way things work out
really impressive
best of luck to both of you guys
smile always
anand:)
thanks a lot for ur support and help:)
ps-by the way do you do some small commerical projects?
i m finding it difficult to do all the coding part and the project is too small to be accepted by any company:)
if you wud be interested it wud be a gr8t help to me also as i will save my time also:)
thanks
anand

Kalibas
06-01-2008, 08:18 PM
Hey guys, I am no programmer, but a bit of handling with google can make incredible results...
I guess this code will do far better and more reliably taking account with Feb 29 etc. Good that php has functions...

date('j.F Y', strtotime('next tuesday', strtotime('now')))


BTW j.n.Y is the way we write in the czech republic, other versions are in the php manual (funciont date) next tuesday can be interchanged with any other day and the value now with any date - then the result will be next tuesday from the date written in.

Medyman
06-01-2008, 11:16 PM
Hey guys, I am no programmer, but a bit of handling with google can make incredible results...
I guess this code will do far better and more reliably taking account with Feb 29 etc. Good that php has functions...

date('j.F Y', strtotime('next tuesday', strtotime('now')))


BTW j.n.Y is the way we write in the czech republic, other versions are in the php manual (funciont date) next tuesday can be interchanged with any other day and the value now with any date - then the result will be next tuesday from the date written in.

The code being discussed here is javascript, not PHP.