Home > Web Front-end > JS Tutorial > Use JS to convert a string into a date and compare the two sizes

Use JS to convert a string into a date and compare the two sizes

怪我咯
Release: 2017-06-29 10:22:26
Original
1512 people have browsed it

This article mainly introduces the JS method of converting string to date and comparing the size. It analyzes the skills of JavaScript string and date operation in detail in the form of examples. It has It must be of reference value. Friends in need can refer to

. This article analyzes the JS method of converting strings to dates and comparing sizes. Share it with everyone for your reference, the details are as follows:

Method 1:

function compareDate(DateOne, DateTwo) {
  var OneMonth = DateOne.substring(5, DateOne.lastIndexOf("-"));
  var OneDay = DateOne.substring(DateOne.length, DateOne.lastIndexOf("-") + 1);
  var OneYear = DateOne.substring(0, DateOne.indexOf("-"));
  var TwoMonth = DateTwo.substring(5, DateTwo.lastIndexOf("-"));
  var TwoDay = DateTwo.substring(DateTwo.length, DateTwo.lastIndexOf("-") + 1);
  var TwoYear = DateTwo.substring(0, DateTwo.indexOf("-"));
  if (Date.parse(OneMonth + "/" + OneDay + "/" + OneYear) > Date.parse(TwoMonth + "/" + TwoDay + "/" + TwoYear)) {
    return true;
  } else {
    return false;
  }
}
Copy after login

Example:

The code is as follows:

alert(compareDate(''2004-12-01'',''2004-05-02'''));
Copy after login

Currently only supported In the format of year-month-day

Method 2:

var checkStartDate = document.form1.checkStartDate.value; 
var checkEndDate = document.form1.checkEndDate.value; 
var arys= new Array(); 
var startdate=new Date(arys[0],parseInt(arys[1]-1),arys[2]); 
if(checkStartDate != null && checkEndDate != null) { 
  arys=checkStartDate.split('-'); 
  var startdate=new Date(arys[0],parseInt(arys[1]-1),arys[2]); 
  arys=checkEndDate.split('-'); 
  var checkEndDate=new Date(arys[0],parseInt(arys[1]-1),arys[2]);  
  if(startdate > checkEndDate) { 
    alert("你的检查日期开始时间大于结束时间啦"); 
    return; 
  }
}
Copy after login

The above is the detailed content of Use JS to convert a string into a date and compare the two sizes. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template