检查一个date是否在两个date之间(javascript)
我需要一件事,我需要检查是否一个date,这是一个string在fomat dd / mm / yyyy是包含在两个date之间,具有相同的格式(dd / mm / yyyy)
我试过这个,但不起作用:
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");
我用debugging器和检查,来自和variables有isNaN值你能帮助我吗? 最好的问候,丹尼尔
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)
比较直接比较date,比较date的getTime()值。 getTime()函数返回1970年1月1日以来的毫秒数,作为一个整数 – 应该是微不足道的,以确定一个整数是否落在两个其他整数之间。
就像是
if((check.getTime() <= to.getTime() && check.getTime() >= from.getTime())) alert("date contained");
试试像下面,它会帮你…
小提琴: http : //jsfiddle.net/RYh7U/146/
脚本:
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; }
有50票的答案不检查date只在几个月的检查。 这个答案是不正确的。 下面的代码工作。
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);
这是在另一个答案发布的代码,我已经改变了date,这就是我注意到这是行不通的
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);
to
和from
variables不是Date对象,因为Date.parse没有成功。 datestring必须符合RFC2822或ISO 8601 。
尝试这个:
HTML
<div id="eventCheck"></div>
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 }
小提琴: https : //jsfiddle.net/zm75cq2a/
尝试这个
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'); }
小提琴