Click to See Complete Forum and Search --> : Anyone have patience for a Newbie?


clankid
05-23-2003, 11:35 AM
I am trying desperately to learn JavaScript on the fly. Dumb I know when all I have is a basic-intermediate understanding of HTML!

I've read over two days all the beginner JS tutorials I can get my hands on in an effort to make this script work on my website:

<!-- Begin
var m=new Array(13);
m[1]="Jan";
m[2]="Feb";
m[3]="Mar";
m[4]="Apr";
m[5]="May";
m[6]="Jun";
m[7]="Jul";
m[8]="Aug";
m[9]="Sep";
m[10]="Oct";
m[11]="Nov";
m[12]="Dec";
var t=new Date();
var m=m[t.getMonth() + 1];
var da=t.getDate();
var y=t.getYear();
if (y < 2000)
y = y + 1900;
document.write(m + " " + da + ", " + y);
// End -->

It's a free date script (as you can see) that I found both in JavaScript Source and a couple other places. The instructions told me to place the script in the body only, there was no head script to include as I think there likely should have been.

Can anyone help me out? Hook a newbie up?

Help! Updating our site daily at work is driving me bonkers fast!

Thanks!
:D

Jona
05-23-2003, 11:36 AM
Yes, put it exactly where you want the date to appear.

pyro
05-23-2003, 11:37 AM
You forgot you opening and closing <script> tags... This works fine:

<script type="text/javascript">
<!-- Begin
var m=new Array(13);
m[1]="Jan";
m[2]="Feb";
m[3]="Mar";
m[4]="Apr";
m[5]="May";
m[6]="Jun";
m[7]="Jul";
m[8]="Aug";
m[9]="Sep";
m[10]="Oct";
m[11]="Nov";
m[12]="Dec";
var t=new Date();
var m=m[t.getMonth() + 1];
var da=t.getDate();
var y=t.getYear();
if (y < 2000)
y = y + 1900;
document.write(m + " " + da + ", " + y);
// End -->
</script>

Jona
05-23-2003, 11:40 AM
...As a personal preference, I would save yourself lines of code like this:


<script type="text/javascript">
<!-- Begin
var m=new Array("Jan","Feb","Mar","Apr","Jun","Jul","Aug","Sep","Oct","Nov","Dec");
var t=new Date();
var m=m[t.getMonth() + 1];
var da=t.getDate();
var y=t.getYear();
if(y<2000) y = y + 1900;
document.write(m + " " + da + ", " + y);
// End -->
</script>

pyro
05-23-2003, 11:46 AM
...Except for the fact that it does not work... ;)

Try this, if you want to do it that way (I included "May" :p, and changed the var m=m[t.getMonth() + 1]; to var m=m[t.getMonth()];

<script type="text/javascript">
<!-- Begin
var m=new Array("Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec");
var t=new Date();
var m=m[t.getMonth()];
var da=t.getDate();
var y=t.getYear();
if(y<2000) y = y + 1900;
document.write(m + " " + da + ", " + y);
// End -->
</script>

clankid
05-23-2003, 11:54 AM
oh my goodness oh my goodness!

Thank you Thank you Thank you!

It works now, it works now!


(laughs evil like) no more daily updates for me!
I KNEW there had to be a better way!


bwaaahhhaaaaaha!

-- no really, thanks everyone.

Amber

Charles
05-23-2003, 11:56 AM
Except that Date.getYear() was depricated back in the last century. And you can shorten up things by using Array literals and by overwriting the native Date.toDateString() method.

<script type="text/javascript">
<!--
Date.prototype.toDateString = function () {return [['Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec'][this.getMonth()], this.getDate() + ',', this.getFullYear()].join(' ')};

alert (new Date().toDateString());
// -->
</script>