Comparing DateTime........please help

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • prashanth

    Comparing DateTime........please help

    Hi all,
    I am unable to write a javascript which will allow me to compare
    datetime entered by the user with the current datetime.( i.e the
    usertime should be less than or greater than currenttime)

    Thanx a lot in advance
  • Brian Genisio

    #2
    Re: Comparing DateTime....... .please help

    prashanth wrote:
    [color=blue]
    > Hi all,
    > I am unable to write a javascript which will allow me to compare
    > datetime entered by the user with the current datetime.( i.e the
    > usertime should be less than or greater than currenttime)
    >
    > Thanx a lot in advance[/color]

    Here is a start... in this example, you can type two forms of the date
    in the box:
    January 20, 2004
    January 20, 2004 10:30:22

    Of course, they need to be entered in exactly, so you will likely set up
    your form for better user input, and create the string yourself.

    The date constructor has the following formats:
    new Date("Month dd, yyyy hh:mm:ss")
    new Date("Month dd, yyyy")
    new Date(yy,mm,dd,h h,mm,ss) // where month is an integer
    new Date(yy,mm,dd)
    new Date(millisecon ds) // milliseconds since January 1, 1970

    Here is the code I came up with... play with it how you need:

    <HTML>
    <HEAD>
    <SCRIPT>
    function compareDate()
    {
    var currentDate = new Date();
    var userDate =
    new Date(document.g etElementById(" user_date").val ue);

    if(userDate <= currentDate)
    alert("SUCCESS" );
    else
    alert("FAILURE" );

    return false;
    }
    </SCRIPT>
    </HEAD>

    <BODY>
    <FORM>
    <INPUT TYPE=text ID=user_date>
    <INPUT TYPE=submit onClick="return compareDate();" >
    </FORM>
    </BODY>
    </HTML>

    Comment

    • Dr John Stockton

      #3
      Re: Comparing DateTime....... .please help

      JRS: In article <400e752a$1@10. 10.0.241>, seen in
      news:comp.lang. javascript, Brian Genisio <BrianGenisio@y ahoo.com> posted
      at Wed, 21 Jan 2004 07:47:40 :-[color=blue]
      >prashanth wrote:[/color]
      [color=blue][color=green]
      >> I am unable to write a javascript which will allow me to compare
      >> datetime entered by the user with the current datetime.( i.e the
      >> usertime should be less than or greater than currenttime)
      >>
      >> Thanx a lot in advance[/color]
      >
      >Here is a start... in this example, you can type two forms of the date
      >in the box:
      >January 20, 2004
      >January 20, 2004 10:30:22
      >
      >Of course, they need to be entered in exactly, so you will likely set up
      >your form for better user input, and create the string yourself.
      >
      >The date constructor has the following formats:
      >new Date("Month dd, yyyy hh:mm:ss")
      >new Date("Month dd, yyyy")
      >new Date(yy,mm,dd,h h,mm,ss) // where month is an integer[/color]
      one smaller than normal[color=blue]
      >new Date(yy,mm,dd)
      >new Date(millisecon ds) // milliseconds since January 1, 1970[/color]
      GMT

      The above is not a complete list.

      OP: you don't give an area-of-use. The code already given will accept,
      I believe, both 17/03/04 and 03/17/04 as St Patrick's Day; but not
      17/03/04, which is what is most generally used for that date.
      Therefore, the form should not be encouraged. 2004/03/17 is safe.

      To validate as being a proper date, see below.

      --
      © John Stockton, Surrey, UK. ?@merlyn.demon. co.uk Turnpike v4.00 IE 4 ©
      <URL:http://jibbering.com/faq/> Jim Ley's FAQ for news:comp.lang. javascript
      <URL:http://www.merlyn.demo n.co.uk/js-index.htm> jscr maths, dates, sources.
      <URL:http://www.merlyn.demo n.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.

      Comment

      Working...