2 ways JavaScript compares the size of two dates

Source: Internet
Author: User

Friends, we have written in the foreground program, is to collect some basic information about users, such as Start time and end time, sometimes in order to improve the effectiveness of the data, the general situation will be used to select the date control to fill out, but Ah, the program is not to solve the human error, If you deliberately choose the start date more than the end date, what do you say? So you have to use JS to do a date comparison operation ... Examples are as follows:
My page now uses a control for the user to select the date, the format of the selected value is: 2009-10-20 14:38:40

A start date, an end date, how to determine the start date can not be greater than the end date?
I didn't say it. Is there two ways of doing it?
First: Convert to date object for comparison operations
<script>
var st= "2009-10-20 14:38:40"
var et= "2009-10-20 15:38:40"
var stdt=new Date (St.replace ("-", "/"));
var etdt=new Date (Et.replace ("-", "/"));
if (STDT>ETDT) alert ("Start time must be less than end time")
</script>
The second: Direct comparison of the size can be
<script>
var st= "2009-10-20 14:38:40"
var et= "2009-10-20 15:38:40"
if (st>et) alert ("Start time must be less than end time")
</script>

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.