SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Zealot
    Join Date
    Mar 2008
    Posts
    129
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Date range verification....

    Hey all. I've got some JS that I wanted to use to display a message banner if the current date falls within a specific range....but the dates that are created for begin/end date are way off and I don't understand how they are being created.

    Here is the code:

    CODE BEGIN
    <html>
    <head>
    <title>Title</title>

    <script language="JavaScript">
    function displayMessageBanner()
    {
    var str1 = "12/01/2008";
    var str2 = "12/29/2008";
    var dt1 = parseInt(str1.substring(0,2),10);
    var mon1 = parseInt(str1.substring(3,5),10);
    var yr1 = parseInt(str1.substring(6,10),10);

    var dt2 = parseInt(str2.substring(0,2),10);
    var mon2 = parseInt(str2.substring(3,5),10);
    var yr2 = parseInt(str2.substring(6,10),10);
    var beginDate = new Date(yr1, mon1, dt1);
    var endDate = new Date(yr2, mon2, dt2);
    var currDate = new Date();

    if(currDate >= beginDate && currDate <= endDate)
    {
    alert("CurrDate: " + currDate + "\n" +
    "Date: " + beginDate);
    return true;
    }
    else
    {
    return false;
    }
    }
    </script>
    </head>
    <body>
    <script language="Javascript">
    var displayActive = displayMessageBanner();
    var displayMessage = "<div style='text-align: center;'>" +
    "<div style='width: 650px; margin: auto; font-size: 14pt; font-weight: bold; border: black solid thin;" +
    "background-color: blue; color: yellow; font-family: arial,helvetica,clean,sans-serif; padding: 5px 5px 5px 5px; " +
    "margin-bottom: 10px;text-align: center;'>" +
    "CHECK FOR SPECIAL CLOSEOUT PRICING ALL MONTH LONG</div></div>";
    if (displayActive == true)
    document.write(displayMessage);
    </script>
    </body>
    </html>
    CODE END

    Any ideas?

  2. #2
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,875
    Mentioned
    25 Post(s)
    Tagged
    1 Thread(s)
    You are forgetting to subtract one from the months before using them to create the date objects.
    Stephen J Chapman

    javascriptexample.net, Book Reviews, follow me on Twitter
    HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
    <input name="html5" type="text" required pattern="^$">

  3. #3
    SitePoint Zealot
    Join Date
    Mar 2008
    Posts
    129
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    That didn't work.

    Why would I need to subtract 1 from the months?


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
  •