SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Zealot litmania_com's Avatar
    Join Date
    Feb 2001
    Location
    London
    Posts
    186
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Undefined error with this javascript - help needed

    I've tried to ammend a script to simply show the date a few days ahead. I came up with the following, but it shows the result as "28 Novemberundefined". I've spent ages, but can't get rid of the "undefined". Could somebody help please? Thank you.

    <script>
    <!-- hide from old browsers
    months = "January,February,March,April,May,June,July,August,September,October,November,December".split(',')
    ;
    daysInTheFuture=3;
    function replyWhen() {
    now = new Date();
    aDayInMilliseconds = 24*60*60*1000;
    then = new Date(now.getTime()+(daysInTheFuture*aDayInMilliseconds));
    document.write(then.getDate() + " " + months[then.getMonth()])

    }

    document.write(replyWhen())

    //-->
    </SCRIPT>

    Nadeem Azam
    Litmania.com Inc.

    Free promotion - check out this 1:1 banner exchange
    Dot com domains - the world's most prestigious domain names for sale

  2. #2
    SitePoint Addict
    Join Date
    May 2000
    Posts
    313
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Maybe this example will help:
    <html>
    <head>
    <title>Add to a date</title>
    <SCRIPT LANGUAGE="JavaScript">
    textmonth=new Array("January","February","March","April","May","June","July","August","September","October","November","December")
    curdate=new Date()
    curmonth=curdate.getMonth()
    curday=curdate.getDate()
    curyear=curdate.getFullYear()
    function adddate(formid)
    {
    if(isNaN(formid.tdays.value)||formid.tdays.value.length==0)
    {
    alert("You must enter a number!")
    formid.tdays.focus()
    formid.tdays.select()
    }
    else
    {
    newday=curday+parseInt(formid.tdays.value)
    formid.tdate.value=newday+" "+textmonth[curmonth]+" "+curyear
    }
    }
    </SCRIPT>
    </head>
    <body>
    <BR><BR>
    <CENTER>
    <FORM NAME="myform">
    Enter Number of Days<INPUT TYPE="text" SIZE="5" NAME="tdays">
    <INPUT TYPE="text" size="30" NAME="tdate">
    <INPUT TYPE="button" VALUE="Get Date" onClick="adddate(this.form)">
    </FORM>
    </CENTER>
    </body>
    </html>

  3. #3
    SitePoint Addict
    Join Date
    May 2000
    Posts
    313
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ignore that first example. That was not well thought out and would not work when you cross months. I believe this example is a better one and hopefully should help.
    <html>
    <head>
    <title>Add to a date</title>
    <SCRIPT LANGUAGE="JavaScript">
    textmonth=new Array("January","February","March","April","May","June","July","August","September","October","November","December")
    curdate=new Date()
    curmonth=curdate.getMonth()
    curday=curdate.getDate()
    curhours=curdate.getHours()
    curmins=curdate.getMinutes()
    cursecs=curdate.getSeconds()
    curmilsecs=curdate.getMilliseconds()
    curyear=curdate.getFullYear()
    function adddate(formid)
    {
    adddays=parseInt(formid.tdays.value) // number of hours to add
    addhours=parseInt(formid.thours.value) // difference between GMT and Local time
    temputc=Date.UTC(curyear,curmonth,curday+adddays,curhours+addhours,curmins,cursecs,curmilsecs)
    newdate=new Date(temputc) // Create date object from milliseconds
    newyear=newdate.getFullYear()
    newmonth=newdate.getMonth()
    newday=newdate.getDate()
    formid.tdate.value=newday+" "+textmonth[newmonth]+" "+newyear
    }
    </SCRIPT>
    </head>
    <body>
    <BR><BR>
    <FORM NAME="myform">
    Enter the number of days to add to the current date: <INPUT TYPE="text" size="4" NAME="tdays"><BR>
    Enter the number of hours difference between GMT and Local time: <INPUT TYPE="text" SIZE="2" NAME="thours"><BR>
    <INPUT TYPE="text" size="30" NAME="tdate">
    <INPUT TYPE="button" VALUE="Get Date" onClick="adddate(this.form)">
    </FORM>
    </body>
    </html>

    Let me know if you have any questions or problems.

  4. #4
    SitePoint Zealot litmania_com's Avatar
    Join Date
    Feb 2001
    Location
    London
    Posts
    186
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thank you 'requestcode', you certainly live up to your name! That works perfectly!

    Thanks once again
    Nadeem Azam
    Litmania.com Inc.

    Free promotion - check out this 1:1 banner exchange
    Dot com domains - the world's most prestigious domain names for sale


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •