检查一次约会是否在两次约会之间

我需要检查一个 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");

我用调试器检查了一下,tofrom变量都有 isNaN值。 你能帮我吗?

245834 次浏览

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

Something like

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

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)

Try what's below. It will help you...

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;
}

试试这个

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');
}

小提琴

有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);

这是另一个答案中的代码,我已经更改了日期,这就是为什么我注意到它不工作

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);

试试这个:

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
}

提琴: Https://jsfiddle.net/zm75cq2a/

下面是用打字稿编写的 Date Prototype 方法:

Date.prototype.isBetween = isBetween;
interface Date { isBetween: typeof isBetween }
function isBetween(minDate: Date, maxDate: Date): boolean {
if (!this.getTime) throw new Error('isBetween() was called on a non Date object');
return !minDate ? true : this.getTime() >= minDate.getTime()
&& !maxDate ? true : this.getTime() <= maxDate.getTime();
};

我做了同样的事情@Diode,第一个答案,但我的条件与一系列的日期,我希望这个例子将是有用的人

E.g (使用日期数组的相同代码)

var dateFrom = "02/06/2013";
var dateTo = "02/09/2013";


var d1 = dateFrom.split("/");
var d2 = dateTo.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 dates= ["02/06/2013", "02/07/2013", "02/08/2013", "02/09/2013", "02/07/2013", "02/10/2013", "02/011/2013"];


dates.forEach(element => {
let parts = element.split("/");
let date= new Date(parts[2], parseInt(parts[1]) - 1, parts[0]);
if (date >= from && date < to) {
console.log('dates in range', date);
}
})

我已经创建了自定义函数来验证给定的日期是否在两个日期之间。

var getvalidDate = function(d){ return new Date(d) }


function validateDateBetweenTwoDates(fromDate,toDate,givenDate){
return getvalidDate(givenDate) <= getvalidDate(toDate) && getvalidDate(givenDate) >= getvalidDate(fromDate);
}

基于已接受的答案的简化方法。

In my case I needed to check if current date (Today) is pithing the range of two other dates so used newDate() instead of hardcoded values but you can get the point how you can use hardcoded dates.


var currentDate = new Date().toJSON().slice(0,10);
var from = new Date('2020/01/01');
var to   = new Date('2020/01/31');
var check = new Date(currentDate);


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


举个例子,假设你的约会是这样的,你需要为提前的约会特性安装 Momentjs。

let cmpDate = Thu Aug 27 2020 00:00:00 GMT+0530 (India Standard Time)


let format = "MM/DD/YYYY";
let startDate: any = moment().format(format);
let endDate: any = moment().add(30, "days").format(format);
let compareDate: any = moment(cmpDate).format(format);
var startDate1 = startDate.split("/");
var startDate2 = endDate.split("/");
var compareDate1 = compareDate.split("/");


var fromDate = new Date(startDate1[2], parseInt(startDate1[1]) - 1, startDate1[0]);
var toDate = new Date(startDate2[2], parseInt(startDate2[1]) - 1, startDate2[0]);
var checkDate = new Date(compareDate1[2], parseInt(compareDate1[1]) - 1, compareDate1[0]);


if (checkDate > fromDate && checkDate < toDate) {
... condition works between current date to next 30 days
}

这个问题非常一般,因此使用日期库的人也会查找答案,但是我找不到日期库的任何答案,因此我将为 Luxon用户提供答案。

const fromDate = '2022-06-01T00:00:00.000Z';
const toDate = '2022-06-30T23:59:59.999Z';
const inputDate = '2022-08-09T20:26:13.380Z';


if (
DateTime.fromISO(inputDate) >= DateTime.fromISO(fromDate) &&
DateTime.fromISO(inputDate) <= DateTime.fromISO(toDate)
) {
console.log('within range');
} else {
console.log('not in range');
}

这可能感觉更直观一些。参数只是一个有效的日期字符串。 如果作为参数传递的日期在当前星期内,则此函数返回 true; 如果不是,则返回 false。

function isInThisWeek(dateToCheck){
// Create a brand new Date instance
const WEEK = new Date()


// create a date instance with the function parameter
//(format should be like dd/mm/yyyy or any javascript valid date format )
const DATEREF = new Date(dateToCheck)


// If the parameter is a not a valid date, return false
if(DATEREF instanceof Date && isNaN(DATEREF)){
console.log("invalid date format")
return false}


// Get separated date infos (the date of today, the current month and the current year) based on the date given as parameter
const [dayR, monthR, yearR] = [DATEREF.getDate(), DATEREF.getMonth(), DATEREF.getFullYear()]


// get Monday date by substracting the day index (number) in the week from the day value (count)
//in the month (like october 15th - 5 (-> saturday index)) and +1 because
//JS weirdly starts the week on sundays
const monday = (WEEK.getDate() - WEEK.getDay()) + 1


// get Saturday date
const sunday = monday + 6


// Start verification
if (yearR !== WEEK.getFullYear())  { console.log("WRONG YEAR"); return false }
if (monthR !== WEEK.getMonth()) { console.log("WRONG MONTH"); return false }
if(dayR >= monday && dayR <= sunday) { return true }
else {console.log("WRONG DAY"); return false}


}