Re: JavaScript question (date, time)

by David Mintz <mambomintz(at)yahoo.com>

 Date:  Wed, 2 Feb 2000 13:01:57 -0800 (PST)
 To:  hwg-languages(at)hwg.org
  todo: View Thread, Original


--- RHoltzworth <rbh97(at)earthlink.net> wrote:
> Hello
> 
>  I am trying to learn JavaScript. I have this form
> I've created, where
> the current day and date appear in the upper left.
> This is fine, except
> I would like the date to use the format mm/dd/yy.

In general, you need to do less to get the number of
the month than you do to get the name of the month,
i.e., you don't need to use getMonth to give you the
index number of the right element of your array of
month names. You just add 1 to the value returned by
getMonth.

Something like:

var today = new Date();
var date = today.getDate();
var month = today.getMonth() + 1;
var year = today.getYear();
// a browser issue...
if (year = 100) { year += 1900 }
var yr = year.toString();
yr = yr.substring(1,3);
var datestring = date + "/" + month + "/" + yr;
document.write("Today is " + datestring);


> Also, I have 2 hidden
> fields where I would like to store the day, date and
> also the current time

var today = new Date();
var hrs = today.getHours();
var min = today.getMinutes();
// assuming the form is called myForm
// and the hidden field's name is time
document.myForm.time.value = hrs + ":" + min;

I'll leave the part about getting the date and 
storing it in a hidden field as an exercise for the
reader.

Also, if the date or month is less than 10, you'll
have  to prepend a zero to it if that's the format you
want. 

And if I've got anything wrong here -- because I'm no 
javascript expert -- then the exercise for the reader
is to shoot me down.

Cheerfully,

      David Mintz
      http://www.panix.com/~dmintz




__________________________________________________
Do You Yahoo!?
Talk to your friends online with Yahoo! Messenger.
http://im.yahoo.com

HWG: hwg-languages mailing list archives, maintained by Webmasters @ IWA