PDA

View Full Version : Help with date script


SDP2006
07-01-2003, 03:59 AM
On my website, there is a market watch that has stock quotes from the previous day. I have a date script that will always show the previous day. As you know, the stocks don't run on Saturdays and Sundays. How can I make the script display on Saturday, Sunday, and Monday, display Friday's date, but on Tuesday display Mondays date??

Thanks

realisis
07-01-2003, 05:04 AM
unless I'm mistaken, this question is related to your existing thread:

http://www.codingforums.com/showthread.php?s=&threadid=22471

If so, I don't know why you felt the need to start a new thread...

<SCRIPT>
<!--
quotes = new Array ( /*blahblah*/ )

now = new Date()
today = now.getDay()

quote = ((today == 6) || (today <= 1))?quotes[5] :quotes[today-1]

document.write(quote)
document.close()
//-->
</SCRIPT>

SDP2006
07-01-2003, 01:32 PM
No, this isn't a quote script, this is a date script.

I forgot to include it last night, it was late. Here is my question again

--On my website, there is a market watch that has stock quotes from the previous day. I have a date script that will always show the previous day. As you know, the stocks don't run on Saturdays and Sundays. How can I make the script display on Saturday, Sunday, and Monday, display Friday's date, but on Tuesday display Mondays date??

Thanks

Here is the code

<!--BEGIN DATE CHANGE JSCRIPT-->
<script type="text/javascript">
var d=new Date()
var weekday=new Array("Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday")
var monthname=new Array("Jan","Feb","Mar","Apr","May","June","July","Aug","Sept","Oct","Nov","Dec")
document.write(monthname[d.getMonth()] + " ")
document.write(d.getDate()-1 + ", ")
document.write(d.getFullYear())
// This JavaScript Written by Stevie Peele
</script>
<!--END SCRIPT-->

SDP2006
07-01-2003, 03:23 PM
Would this work? I done some editing myself. It has no errors, but I don't know if it works

<script type="text/javascript">
var d=new Date()
var weekday=new Array("Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday")
var monthname=new
Array("Jan","Feb","Mar","Apr","May","June","July","Aug","Sept","Oct","Nov","Dec")
document.write(monthname[d.getMonth()] + " ")
document.write(d.getDate() + ", ")
document.write(d.getFullYear())
if (d.getDay == 1)
{
document.write(monthname[d.getMonth()] + " ")
document.write(d.getDate()-2 + ", ")
document.write(d.getFullYear())
}
if (d.getDay == 2)
{
document.write(monthname[d.getMonth()] + " ")
document.write(d.getDate()-3 + ", ")
document.write(d.getFullYear())
}
</script>


How are days in JavaScript. Is 1 Sunday or is 1 Monday.

beetle
07-01-2003, 03:34 PM
Sunday is 0, Monday is 1, etc.

Danne
07-01-2003, 03:53 PM
Try this (if yours doesn't work):



Date.prototype.getDaysInMonth=function() {
switch (this.getMonth()) {
case 0:return 31;
case 2:return 31;
case 4:return 31;
case 6:return 31;
case 7:return 31;
case 9:return 31;
case 11:return 31;
case 3:return 30;
case 5:return 30;
case 8:return 30;
case 10:return 30;
case 1:
if (this.getFullYear()%4==0)
return 29;
else
return 28;
}
}

function getStockDate(d) {
//var weekday=new Array("Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday");
var monthname=new Array("Jan","Feb","Mar","Apr","May","June","July","Aug","Sept","Oct","Nov","Dec");
var day=d.getDate();
do {
var wDay=d.getDay();
if (wDay<2) { // Sunday or Monday
day-=(wDay+2);
}else {
day--; // Yesterday
}
if (day<1) {
var month=d.getMonth()-1;
if (month<0) {
month=11;
d.setFullYear(d.getFullYear()-1);
}
d.setMonth(month);
day=d.getDaysInMonth();
}
d.setDate(day);
wDay=d.getDay();
} while (wDay==0||wDay==6);
return monthname[d.getMonth()] + " "
+ d.getDate() + ", "
+ d.getFullYear();
}
document.write(getStockDate(new Date())+'<br>'); // Today
document.write(getStockDate(new Date(2004, 2, 1))+'<br>'); // March 1
document.write(getStockDate(new Date(2000, 2, 1))+'<br>'); // March 1
document.write(getStockDate(new Date(2003, 0, 1))+'<br>'); // Jan 1



There may of course be other days when the stock is closed, so it's not really reliable.

beetle
07-01-2003, 04:13 PM
Danne, you can compres your switch statement quite a bitDate.prototype.getDaysInMonth=function() {
switch (this.getMonth()) {
case 0:
case 2:
case 4:
case 6:
case 7:
case 9:
return 31;
case 1:
return (this.getFullYear()%4==0) ? 29 : 28;
default: return 30;
}
}
However, the easiet way to do this calucation isvar d = new Date();
var dayOfWeek = d.getDay();
var diff = ( dayOfWeek < 2 ) ? dayOfWeek - 5 : 1;
d.setDate( d.getDate() - diff );Now d is always the current date minus 1, except Sunday and Monday always point at Friday.

:thumbsup:

Danne
07-01-2003, 04:25 PM
Yeah, that certainly looks better...:)

But what about the first in the month,
1 of Jan less one means 31 of Dec...

Or does the Date object handle that automatically?

beetle
07-01-2003, 04:39 PM
The Date object handles that for you :thumbsup:

Danne
07-01-2003, 05:03 PM
Well that sounds really good...:)

But when I tried your code with my examples, I got a different result on March 1, 2004:

I used your code like this:

function getStockDate(d) {
var monthname=new Array("Jan","Feb","Mar","Apr","May","June","July","Aug","Sept","Oct","Nov","Dec");
var dayOfWeek = d.getDay();
var diff = ( dayOfWeek < 2 ) ? dayOfWeek - 5 : 1;
d.setDate( d.getDate() - diff );
return monthname[d.getMonth()] + " "
+ d.getDate() + ", "
+ d.getFullYear();
}

document.write(getStockDate(new Date())+'<br>'); // Today
document.write(getStockDate(new Date(2004, 2, 1))+'<br>'); // March 1 (Monday)
document.write(getStockDate(new Date(2000, 2, 1))+'<br>'); // March 1 (Wednesday)
document.write(getStockDate(new Date(2003, 0, 1))+'<br>'); // Jan 1 (Wednesday)



I got these results with my code, which is correct as far as I checked:
June 30, 2003
Feb 27, 2004
Feb 29, 2000
Dec 31, 2002

...and these with the code above:
June 30, 2003
Mar 5, 2004
Feb 29, 2000
Dec 31, 2002


Nevermind I found the bug:

function getStockDate(d) {
var monthname=new Array("Jan","Feb","Mar","Apr","May","June","July","Aug","Sept","Oct","Nov","Dec");
var dayOfWeek = d.getDay();
var diff = ( dayOfWeek < 2 ) ? dayOfWeek - 5 : 1;
d.setDate( d.getDate() - (diff<0 ? -diff-1 : diff) );
return monthname[d.getMonth()] + " "
+ d.getDate() + ", "
+ d.getFullYear();
}


That last piece should work..:thumbsup:

beetle
07-01-2003, 05:11 PM
Ah yes, I see the blip. You can accomplish that on one line

instead of

var diff = ( dayOfWeek < 2 ) ? dayOfWeek - 5 : 1;

use this

var diff = ( dayOfWeek < 2 ) ? -(dayOfWeek - 5) : 1;

Danne
07-01-2003, 05:49 PM
Originally posted by beetle
Ah yes, I see the blip. You can accomplish that on one line

instead of

var diff = ( dayOfWeek < 2 ) ? dayOfWeek - 5 : 1;

use this

var diff = ( dayOfWeek < 2 ) ? -(dayOfWeek - 5) : 1;


Or this:
var diff = ( dayOfWeek < 2 ) ? dayOfWeek + 2 : 1;

:confused: :D

beetle
07-01-2003, 06:27 PM
You sure?

My way:
-(0 - 5) == 5
-(1 - 5) == 4

Your way
0 + 2 == 2
1 + 2 == 3

Bit if a discrepancy, no?

Danne
07-01-2003, 06:42 PM
Quite, but look at the result of the following code:


function getStockDate(d) {
var monthname=new Array("Jan","Feb","Mar","Apr","May","June","July","Aug","Sept","Oct","Nov","Dec");
var dayOfWeek = d.getDay();
var diff = ( dayOfWeek < 2 ) ? -(dayOfWeek - 5) : 1;
d.setDate( d.getDate() - diff );
return monthname[d.getMonth()] + " "
+ d.getDate() + ", "
+ d.getFullYear();
}
function getStockDate2(d) {
var monthname=new Array("Jan","Feb","Mar","Apr","May","June","July","Aug","Sept","Oct","Nov","Dec");
var dayOfWeek = d.getDay();
var diff = ( dayOfWeek < 2 ) ? dayOfWeek + 2 : 1;
d.setDate( d.getDate() - diff );
return monthname[d.getMonth()] + " "
+ d.getDate() + ", "
+ d.getFullYear();
}

document.write(getStockDate(new Date(2003, 5, 1))+'<br>'); // June 1 (Sunday)
document.write(getStockDate(new Date(2004, 2, 1))+'<br>'); // March 1 (Sunday)
document.write('<br>');
document.write(getStockDate2(new Date(2003, 5, 1))+'<br>'); // June 1 (Sunday)
document.write(getStockDate2(new Date(2004, 2, 1))+'<br>'); // March 1 (Sunday)

Result:
May 27, 2003
Feb 26, 2004

May 30, 2003
Feb 27, 2004



May 27, 2003 is a Tuesday and
Feb 26, 2004 is a Thursday....