Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7

Thread: Date Problem

  1. #1
    New Coder
    Join Date
    Jun 2002
    Location
    Texas
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Date Problem

    I am trying to take a date that is keyed in and compare it to today's date. If the date keyed is more than one day PREVIOUS to today's date, I want to show an alert.
    What I am getting is, if the date is tomorrow or beyond, then it passes. If the date is today or less, I get the alert.

    For the page below, click on Renew and enter any number in to the membership number and click on submit. This will allow you to bypass most of the required fields. The next field required is the date field in question.

    http://home.earthlink.net/~optimum/ccprc101test4.htm


    Please help.
    Thanks

  • #2
    Senior Coder
    Join Date
    Jun 2002
    Location
    frankfurt, german banana republic
    Posts
    1,848
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Optimum, after hitting submit I get to "page not found" error page.

    Anyway, let's try to answer your question. It depends of course also on how you initialize the two date objects you want to compare. Here's a function that might help you along, it simply compares both date's miliseconds.

    Code:
    function checkDate(dateYear, dateMonth, dateDay) {
        var enteredDate = new Date(dateYear, dateMonth, dateDay);
        var today = new Date();
        
        if (today.getTime() - (1000 * 60 * 60 * 24) <= enteredDate.getTime()) {
            alert("not valid");
        } else {
            alert("ok");
        }
    }
    
    checkDate(2002, 7, 21); //  that's ok 
    checkDate(2002, 7, 22); //  that isn't
    hth

  • #3
    New Coder
    Join Date
    Jun 2002
    Location
    Texas
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks Mordred,

    Still confused as ever and my JavaScript book is not much help either. I just want to subtract the date entered from today's date and if it is greater than one, show an alert.

    Can someone please tell me why the following doesn't work?


    var str = document.enrollfm.startdate.value; // date entered
    var now = new Date();
    var today = new Date(now.getYear(),now.getMonth(),now.getDate());
    var century = parseInt(now.getYear()/100)*100;
    if ((now.getYear()%100)>=parseInt(str.substring(6,8)))
    {
    var date = new Date(century+parseInt(str.substring(4,6)),
    parseInt(str.substring(0,2)),
    str.substring(3,5));
    }
    else
    {
    var date = new Date(century-100+parseInt(str.substring(6,8)),
    parseInt(str.substring(0,2)),
    str.substring(3,5));
    }

    var datecheck = today - date;

    if (datecheck > 1){
    alert('Start Date cannot be more than 1 day previous to Today.');
    al.focus();
    return false;
    }

    Thanks for any help.

  • #4
    Regular Coder
    Join Date
    Jun 2002
    Location
    Round Rock, Texas
    Posts
    443
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Ever hear the ditty for the yellow pages, "let your fingers do the walking?" Well, let your dates do the walking.

    OK, that doesn't make sense but the point is you should use the built-in date functions effectively and not do all that convoluted manipulation.

    My understanding of the getDate/Month/Year functions is that they return integers, not strings. No need to "parseInt()".

    You should not have to work with dates in their milisecond forms. You can do date arithmetic without it.

    You should be able to do things like this:

    var today = new Date();
    var yesterday = today;
    yesterday.setDate(today.getDate()-1); // now it's really yesterday.

    and stuff like this with date objects:
    if (date1 <= yesterday)...

    getYear can be problematic. It returns 2 digits if the year is 1900-1999 inclusive and a 4 digits if any other year.

    There is a new function "getFullYear()" which always returns a 4 digit year, but it's not implimented in JS 1.2
    http://developer.netscape.com/docs/m...f/contents.htm

    Just think about dates objects as being able to "take care of themselves". I.E. you don't have to convert them to some "base" type (miliseconds, integers, whatever) to manipulate them; and don't worry about crossing over a month or year as you do arithmetic on dates.

  • #5
    Senior Coder joh6nn's Avatar
    Join Date
    Jun 2002
    Location
    72° W. 48' 57" , 41° N. 32' 04"
    Posts
    1,887
    Thanks
    0
    Thanked 1 Time in 1 Post
    Originally posted by RadarBob
    var today = new Date();
    var yesterday = today;
    yesterday.setDate(today.getDate()-1); // now it's really yesterday.
    dude, dates are objects. objects are referenced. var yesterday = today, does not create a new object, it creates another way to get a handle on the today Date object. in order to get a second date, that's yesterday, you need to do something like this:

    var today = new Date();
    var yesterday = new Date();
    yesterday.setDate(today.getDate()-1); // now it's really yesterday.
    bluemood | devedge | devmo | MS Dev Library | WebMonkey | the Guide

    i am a loser geek, crazy with an evil streak,
    yes i do believe there is a violent thing inside of me.

  • #6
    New Coder
    Join Date
    Jun 2002
    Location
    Texas
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts
    The date entered is in the format of MM/DD/YY. The substringing is to put it in a YYYY/MM/DD format so that the arithmetic can work properly. At least I think.

    Is there a way to take just the whole day portion of the today's date and compare that to the whole day portion of the entered date? Ex. 2002/08/25 - 2002/08/24

    If I use Year/Month/Day HH.MM.SS milliseconds then I can see how the date and time combination can be less than I think.
    An example of this would be
    2002/08/25 10.25.15 125000 - 2002/08/24 10.25.16 125000 Wouldn't this be less than one?

    However, if I just use the Year/Month/Day portion then I would get at least one(1).

    My original problem is that if the date entered is yesterday, it's good. If the date entered is before yesterday, it's bad.

  • #7
    Regular Coder
    Join Date
    Jun 2002
    Location
    Round Rock, Texas
    Posts
    443
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Originally posted by optimum
    My original problem is that if the date entered is yesterday, it's good. If the date entered is before yesterday, it's bad.
    OK, let me try this again...

    Code:
    function ElimiDate (userEnteredDate) {
       var goodDate = false;
       var today = new Date();
       var yesterday = new Date();
       yesterday.setDate(today.getDate()-1)  // now it's really yesterday
    
       if (userEnteredDate < yesterday) {
          goodDate = false;
       }else{
          goodDate = true;
       }
       return goodDate;
    }


  •  

    Posting Permissions

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