Js time comparison example

Source: Internet
Author: User
Tags date1

Js time comparison example

This article mainly introduces the js time comparison example. The code is simple and you can see the result after running it. For more information, see

 

The Code is as follows:

<Html>

<Head>

<Script language = "javascript" type = "text/javascript">

/** Date comparison **/

Function compareDate (strDate1, strDate2)

{

Var date1 = new Date (strDate1.replace (/-/g ,"/"));

Var date2 = new Date (strDate2.replace (/-/g ,"/"));

Return date1-date2;

}

 

/** Compare **/

Function doCompare (){

Var strDate1 = document. getElementById ("strDate1"). value;

Var strDate2 = document. getElementById ("strDate2"). value;

Var result = compareDate (strDate1, strDate2 );

If (result> 0 ){

Alert ("strDate1 later than strDate2 ");

} Else if (result <0 ){

Alert ("strDate1 earlier than strDate2 ");

} Else if (result = 0 ){

Alert ("strDate1 equals strDate2 ");

}

}

</Script>

</Head>

<Body>

<Input type = "text" id = "strDate1" name = "strDate1" value = "2012-07-01"/>

<Input type = "text" id = "strDate2" name = "strDate2" value = "2012-08-01"/>

<Input type = "button" id = "compareBtn" name = "compareBtn" value = "Compare" onClick = "doCompare ();"/>

</Body>

</Html>

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.