Results 1 to 5 of 5

Thread: Validate Same Month

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 2000
    Location
    Tennessee
    Posts
    279

    Validate Same Month

    How can I validate in Javascript that two dates are within the same month without comparing the first two characters of the date string (i.e. 05/11/2004)?
    A cynic knows the price of everything but the value of nothing.

  2. #2
    Frenzied Member Acidic's Avatar
    Join Date
    Sep 2003
    Location
    UK
    Posts
    1,090
    what form is the input in?
    if it is dd/mm/yyyy or mm/dd/yyyy, then you have to compare the mm to the other mm. Of course there are more and less efficient ways to do this.
    Here's how I'd do it.
    Code:
    date1 = "24/02/1986"
    date2 = "11/05/2004"
    Ar1 = new Array()
    Ar2 = new Array()
    Ar1 = split(date1,"/")
    Ar2 = split(date2,"/")
    if (Ar1[1] == Ar2[1])
      {
      //Same month
      }
    Have I helped you? Please Rate my posts.

  3. #3
    Don't Panic! Ecniv's Avatar
    Join Date
    Nov 2000
    Location
    Amsterdam...
    Posts
    5,343
    Dredging up a memory of javascript, there is a date type. If you can get the entered date into it I think there is a property or method of .month()


    Vince

    BOFH Now, BOFH Past, Information on duplicates

    Feeling like a fly on the inside of a closed window (Thunk!)
    If I post a lot, it is because I am bored at work! ;D Or stuck...
    * Anything I post can be only my opinion. Advice etc is up to you to persue...

  4. #4
    Frenzied Member Acidic's Avatar
    Join Date
    Sep 2003
    Location
    UK
    Posts
    1,090
    Code:
    //To get the current month:
    now = new Dat()
    current_month = now.getMonth()
    That will get you the current month, but this isn't very useful for comparing two dates. It could be if one of the dates is always todays date I suppose. You'll still have to use the mm from dd/mm/yyyy from the other date though.
    If you're simply trying to compare two dates, use the code I supplied in the first post.
    Have I helped you? Please Rate my posts.

  5. #5
    Don't Panic! Ecniv's Avatar
    Join Date
    Nov 2000
    Location
    Amsterdam...
    Posts
    5,343
    Code:
    //To get the current month:
    now = new Dat()
    current_month = now.getMonth()
    Following on...
    Code:
    strDate1 = "24/02/1986"
    strDate2 = "11/05/2004"
    
    // dunno if this works? or how it copes with american/english date formats
    dte1 = strDate1.toDate()
    dte2 = strDate2.toDate()
    
    if (dte1.getMonth() == dte2.getMonth()) {
    //do whatever
    }
    But hey its your choice, we are just providing options


    Vince

    BOFH Now, BOFH Past, Information on duplicates

    Feeling like a fly on the inside of a closed window (Thunk!)
    If I post a lot, it is because I am bored at work! ;D Or stuck...
    * Anything I post can be only my opinion. Advice etc is up to you to persue...

Posting Permissions

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



Click Here to Expand Forum to Full Width