Date-time comparison code in JavaScript

Source: Internet
Author: User

For example, date 1 is 2009-8-1, date 2 is 2009-7-1.

Using the Split function to slice,

Method 1

The code is as follows Copy Code

<script>
var d1= "2009-8-1";
var d2= "2009-7-15";
D1arr=d1.split ('-');
D2arr=d2.split ('-');
V1=new Date (d1arr[0],d1arr[1],d1arr[2]);
V2=new Date (d2arr[0],d2arr[1],d2arr[2]);
alert (V1>V2);
</script>

Method 2

JS Date comparison (YYYY-MM-DD)

The code is as follows Copy Code

function Duibi (A, b) {
var arr = a.split ("-");
var starttime = new Date (arr[0], arr[1], arr[2]);
var starttimes = Starttime.gettime ();

var arrs = B.split ("-");
var lktime = new Date (Arrs[0], arrs[1], arrs[2]);
var lktimes = Lktime.gettime ();

if (starttimes >= lktimes) {

Alert (' Start time is greater than departure time, please check ');
return false;
}
Else
return true;

}

Example 1

The code is as follows Copy Code

<script language= "JavaScript" >
function test (STR1,STR2)
{
var D1 = new Date (STR1);
var d2 = new Date (STR2);
if (Date.parse (D1)-Date.parse (D2) ==0)
{
Alert (str1+ "equals" +str2)
}
if (Date.parse (D1)-Date.parse (D2) <0)
{
Alert (str1+ "earlier than" +STR2)
}
if (Date.parse (D1)-Date.parse (D2) >0)
{
Alert (str1+ "later than" +STR2)
}
}
</script>
<input type= "button" value= "OK" onclick= "Test (' 2002/4/12 ', ' 2006/4/12 ')" >


JS Time comparison (Yyyy-mm-dd Hh:mi:ss) with the operation of the banknote

The code is as follows Copy Code

function Comptime () {
var begintime = "2009-09-21 00:00:00";
var endtime = "2009-09-21 00:00:01";
var begintimes = begintime.substring (0). Split ('-');
var endtimes = endtime.substring (0). Split ('-');

BeginTime = begintimes[1] + '-' + begintimes[2] + '-' + begintimes[0] + ' + begintime.substring (10, 19);
Endtime = endtimes[1] + '-' + endtimes[2] + '-' + endtimes[0] + ' + endtime.substring (10, 19);

    alert (begintime + "AAA" + endtime);
    alert (Date.parse (Endtime));
    alert (Date.parse (begintime));
    var a = (Date.parse (endtime)-Date.parse (BeginTime))/3600/1000;
    if (a < 0) {
        alert ("Endtime small!");
   } else if (a > 0) {
        alert ("Endtime big!");
   } else if (a = = 0) {
        alert ("Equal time!");
   } else {
        return ' exception '
    }
}

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.