检查一个日期是否在两个日期之间(javascript)

时间:2023-02-08 08:40:22

I need one thing, I need check if one date, that is a string in fomat dd/mm/yyyy is contained between two dates, with the same format (dd/mm/yyyy)

我需要一件事,我需要检查一个日期,即fomat dd / mm / yyyy中的字符串是否包含在两个日期之间,格式相同(dd / mm / yyyy)

I tried this, but doesn't work:

我试过这个,但是不起作用:

var dateFrom = "02/05/2013";
var dateTo = "02/09/2013";
var dateCheck = "02/07/2013";

var from = Date.parse(dateFrom);
var to   = Date.parse(dateTo);
var check = Date.parse(dateCheck );

if((check <= to && check >= from))      alert("date contained");

I used debugger and check,to and from variables have isNaN value Could you help me?

我用调试器和检查,来往变量有isNaN值你能帮帮我吗?

6 个解决方案

#1


64  

Date.parse supports the format mm/dd/yyyy not dd/mm/yyyy. For that either you have to use a library like moment.js or do something as given below

Date.parse支持格式mm / dd / yyyy而不是dd / mm / yyyy。为此,您必须使用像moment.js这样的库,或者执行下面给出的操作

var dateFrom = "02/05/2013";
var dateTo = "02/09/2013";
var dateCheck = "02/07/2013";

var d1 = dateFrom.split("/");
var d2 = dateTo.split("/");
var c = dateCheck.split("/");

var from = new Date(d1[2], parseInt(d1[1])-1, d1[0]);  // -1 because months are from 0 to 11
var to   = new Date(d2[2], parseInt(d2[1])-1, d2[0]);
var check = new Date(c[2], parseInt(c[1])-1, c[0]);

console.log(check > from && check < to)

#2


20  

Instead of comparing the dates directly, compare the getTime() value of the date. The getTime() function returns the number of milliseconds since Jan 1, 1970 as an integer-- should be trivial to determine if one integer falls between two other integers.

而不是直接比较日期,比较日期的getTime()值。 getTime()函数返回自1970年1月1日以来作为整数的毫秒数 - 确定一个整数是否落在另外两个整数之间应该是微不足道的。

Something like

就像是

if((check.getTime() <= to.getTime() && check.getTime() >= from.getTime()))      alert("date contained");

#3


9  

Try like below, It will hep you...

尝试如下,它会让你...

Fiddle : http://jsfiddle.net/RYh7U/146/

小提琴:http://jsfiddle.net/RYh7U/146/

Script :

脚本:

if(dateCheck("02/05/2013","02/09/2013","02/07/2013"))
    alert("Availed");
else
    alert("Not Availed");

function dateCheck(from,to,check) {

    var fDate,lDate,cDate;
    fDate = Date.parse(from);
    lDate = Date.parse(to);
    cDate = Date.parse(check);

    if((cDate <= lDate && cDate >= fDate)) {
        return true;
    }
    return false;
}

#4


4  

The answer that has 50 votes doesn't check for date in only checks for months. That answer is not correct. The code below works.

有50票的答案不会在几个月的支票中检查日期。答案不正确。以下代码有效。

var dateFrom = "01/08/2017";
var dateTo = "01/10/2017";
var dateCheck = "05/09/2017";

var d1 = dateFrom.split("/");
var d2 = dateTo.split("/");
var c = dateCheck.split("/");

var from = new Date(d1);  // -1 because months are from 0 to 11
var to   = new Date(d2);
var check = new Date(c);

alert(check > from && check < to);

This is the code posted in another answer and I have changed the dates and that's how I noticed it doesn't work

这是在另一个答案中发布的代码,我更改了日期,这就是我注意到它不起作用的方式

var dateFrom = "02/05/2013";
var dateTo = "02/09/2013";
var dateCheck = "07/07/2013";

var d1 = dateFrom.split("/");
var d2 = dateTo.split("/");
var c = dateCheck.split("/");

var from = new Date(d1[2], parseInt(d1[1])-1, d1[0]);  // -1 because months are from 0 to 11
var to   = new Date(d2[2], parseInt(d2[1])-1, d2[0]);
var check = new Date(c[2], parseInt(c[1])-1, c[0]);


alert(check > from && check < to);

#5


0  

Try this:

尝试这个:

HTML

HTML

<div id="eventCheck"></div>

JAVASCRIPT

JAVASCRIPT

// ----------------------------------------------------//
// Todays date
var today = new Date();
var dd = today.getDate();
var mm = today.getMonth()+1; //January is 0!
var yyyy = today.getFullYear();

// Add Zero if it number is between 0-9
if(dd<10) {
    dd = '0'+dd;
}
if(mm<10) {
    mm = '0'+mm;
}

var today = yyyy + '' + mm + '' + dd ;


// ----------------------------------------------------//
// Day of event
var endDay = 15; // day 15
var endMonth = 01; // month 01 (January)
var endYear = 2017; // year 2017

// Add Zero if it number is between 0-9
if(endDay<10) {
    endDay = '0'+endDay;
} 
if(endMonth<10) {
    endMonth = '0'+endMonth;
}

// eventDay - date of the event
var eventDay = endYear + '/' + endMonth + '/' + endDay;
// ----------------------------------------------------//



// ----------------------------------------------------//
// check if eventDay has been or not
if ( eventDay < today ) {
    document.getElementById('eventCheck').innerHTML += 'Date has passed (event is over)';  // true
} else {
    document.getElementById('eventCheck').innerHTML += 'Date has not passed (upcoming event)'; // false
}

Fiddle: https://jsfiddle.net/zm75cq2a/

小提琴:https://jsfiddle.net/zm75cq2a/

#6


-1  

Try this

尝试这个

var gdate='01-05-2014';
        date =Date.parse(gdate.split('-')[1]+'-'+gdate.split('-')[0]+'-'+gdate.split('-')[2]);
        if(parseInt(date) < parseInt(Date.now()))
        {
            alert('small');
        }else{
            alert('big');
        }

Fiddle

小提琴

#1


64  

Date.parse supports the format mm/dd/yyyy not dd/mm/yyyy. For that either you have to use a library like moment.js or do something as given below

Date.parse支持格式mm / dd / yyyy而不是dd / mm / yyyy。为此,您必须使用像moment.js这样的库,或者执行下面给出的操作

var dateFrom = "02/05/2013";
var dateTo = "02/09/2013";
var dateCheck = "02/07/2013";

var d1 = dateFrom.split("/");
var d2 = dateTo.split("/");
var c = dateCheck.split("/");

var from = new Date(d1[2], parseInt(d1[1])-1, d1[0]);  // -1 because months are from 0 to 11
var to   = new Date(d2[2], parseInt(d2[1])-1, d2[0]);
var check = new Date(c[2], parseInt(c[1])-1, c[0]);

console.log(check > from && check < to)

#2


20  

Instead of comparing the dates directly, compare the getTime() value of the date. The getTime() function returns the number of milliseconds since Jan 1, 1970 as an integer-- should be trivial to determine if one integer falls between two other integers.

而不是直接比较日期,比较日期的getTime()值。 getTime()函数返回自1970年1月1日以来作为整数的毫秒数 - 确定一个整数是否落在另外两个整数之间应该是微不足道的。

Something like

就像是

if((check.getTime() <= to.getTime() && check.getTime() >= from.getTime()))      alert("date contained");

#3


9  

Try like below, It will hep you...

尝试如下,它会让你...

Fiddle : http://jsfiddle.net/RYh7U/146/

小提琴:http://jsfiddle.net/RYh7U/146/

Script :

脚本:

if(dateCheck("02/05/2013","02/09/2013","02/07/2013"))
    alert("Availed");
else
    alert("Not Availed");

function dateCheck(from,to,check) {

    var fDate,lDate,cDate;
    fDate = Date.parse(from);
    lDate = Date.parse(to);
    cDate = Date.parse(check);

    if((cDate <= lDate && cDate >= fDate)) {
        return true;
    }
    return false;
}

#4


4  

The answer that has 50 votes doesn't check for date in only checks for months. That answer is not correct. The code below works.

有50票的答案不会在几个月的支票中检查日期。答案不正确。以下代码有效。

var dateFrom = "01/08/2017";
var dateTo = "01/10/2017";
var dateCheck = "05/09/2017";

var d1 = dateFrom.split("/");
var d2 = dateTo.split("/");
var c = dateCheck.split("/");

var from = new Date(d1);  // -1 because months are from 0 to 11
var to   = new Date(d2);
var check = new Date(c);

alert(check > from && check < to);

This is the code posted in another answer and I have changed the dates and that's how I noticed it doesn't work

这是在另一个答案中发布的代码,我更改了日期,这就是我注意到它不起作用的方式

var dateFrom = "02/05/2013";
var dateTo = "02/09/2013";
var dateCheck = "07/07/2013";

var d1 = dateFrom.split("/");
var d2 = dateTo.split("/");
var c = dateCheck.split("/");

var from = new Date(d1[2], parseInt(d1[1])-1, d1[0]);  // -1 because months are from 0 to 11
var to   = new Date(d2[2], parseInt(d2[1])-1, d2[0]);
var check = new Date(c[2], parseInt(c[1])-1, c[0]);


alert(check > from && check < to);

#5


0  

Try this:

尝试这个:

HTML

HTML

<div id="eventCheck"></div>

JAVASCRIPT

JAVASCRIPT

// ----------------------------------------------------//
// Todays date
var today = new Date();
var dd = today.getDate();
var mm = today.getMonth()+1; //January is 0!
var yyyy = today.getFullYear();

// Add Zero if it number is between 0-9
if(dd<10) {
    dd = '0'+dd;
}
if(mm<10) {
    mm = '0'+mm;
}

var today = yyyy + '' + mm + '' + dd ;


// ----------------------------------------------------//
// Day of event
var endDay = 15; // day 15
var endMonth = 01; // month 01 (January)
var endYear = 2017; // year 2017

// Add Zero if it number is between 0-9
if(endDay<10) {
    endDay = '0'+endDay;
} 
if(endMonth<10) {
    endMonth = '0'+endMonth;
}

// eventDay - date of the event
var eventDay = endYear + '/' + endMonth + '/' + endDay;
// ----------------------------------------------------//



// ----------------------------------------------------//
// check if eventDay has been or not
if ( eventDay < today ) {
    document.getElementById('eventCheck').innerHTML += 'Date has passed (event is over)';  // true
} else {
    document.getElementById('eventCheck').innerHTML += 'Date has not passed (upcoming event)'; // false
}

Fiddle: https://jsfiddle.net/zm75cq2a/

小提琴:https://jsfiddle.net/zm75cq2a/

#6


-1  

Try this

尝试这个

var gdate='01-05-2014';
        date =Date.parse(gdate.split('-')[1]+'-'+gdate.split('-')[0]+'-'+gdate.split('-')[2]);
        if(parseInt(date) < parseInt(Date.now()))
        {
            alert('small');
        }else{
            alert('big');
        }

Fiddle

小提琴