Check If One Date Is Between Two Dates

Check if one date is between two dates

Date.parse supports the format mm/dd/yyyy not dd/mm/yyyy. For the latter, either use a library like moment.js or do something as shown below

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)

How to check if current date is between two dates in javascript

You can get a new date and compare it against your stored dates (though you appear to have not stored the dates as correct date-timestamps). The item you are after is the one with a startDate less than current and end endDate greater than current. This will also give an empty array if no matches found - so you will need to have a check against the returned matches length.

const programs = [
{ video: "URL1", startDate: 1644492517146, endDate: 1644492517446 },
{ video: "URL2", startDate: 1644492517726, endDate: 1644492517646 },
{ video: "URL3", startDate: 1644492507746, endDate: 1644492517546 },
{ video: "URL4", startDate: 1644492517746, endDate: 16474492519746 }
];

const now =new Date().getTime(); // gives similar to 1644492517146

const currentProgram = programs.filter(
p => new Date(p.startDate) < now && new Date(p.endDate) > now
);

console.log(currentProgram) // gives [{ video: "URL4", startDate: 1644492517746, endDate: 16474492519746 }]

Check if ISO String date is between two dates

The main idea is we just need to compare Date only.

However, if you use .moment or new Date() to parse the date-time UTC, you will recognize an interesting that

moment("2021-01-31T22:18:46Z").format("YYYY-MM-DD") // "2021-02-01"
new Date("2021-01-31T22:18:46Z") // Mon Feb 01 2021 05:18:46 GMT+0700 (Indochina Time)

So if the compareDate like 2021-01-31T05:00:00.000Z, then you will get the expected result.

const fromDate = moment("2021-01-30T05:00:00.000Z").format("YYYY-MM-DD");
const conpareDate = moment("2021-01-31T05:00:00.000Z").format("YYYY-MM-DD");
const toDate = moment("2021-01-31T05:00:00.000Z").format("YYYY-MM-DD");

console.log({fromDate, conpareDate, toDate});
console.log(moment(conpareDate).isBetween(fromDate, toDate, undefined, '(]'));
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.29.1/moment.min.js"></script>

check if date is between two dates without year

You only wish to compare on the m and d annually, so only create a string to compare against that consists of m-d. Then you can shorten your start and end dates to only be m-d as well. Because you have formatted your dates from bigger unit to smaller unit (month then day), you can simply compare the dates as regular strings.

Code: (Demo)

$now = date('m-d');
$start_md = "12-06";
$end_md = "12-27";

if ($start_md <= $now && $now <= $end_md) {
echo "Christmas time";
} else {
echo "Not christmas time";
}

Check if a date is between two dates

Firstly, create a new Date() from your currentDate ( currentDate is string format Y-d-m with hour and minutes is 00:00)

var current = new Date(currentDate);
var matchWithCurrent = [];
var matchWithDayBack = [];
var between = [];

just loop through your date array

for (var i=0; i<dates.length; i++) {

var tmpDate = new Date(dates[i]); //Convert string to date
var diff = Math.ceil((current - tmpDate) / (1000 * 3600 * 24)); //get time difference (current - tmpDate is milisecond, we need convert it to day)

// Check condition and push it on array correct

if (diff == 0) {
matchWithCurrent.push(dates[i]);
}

if (diff == 14) {
matchWithDayBack.push(dates[i]);
}

if ((diff > 0) && (diff <14)) {
between.push(dates[i]);
}

}

console.log(matchWithCurrent);
console.log(matchWithDayBack);
console.log(between);

If you want only one array match with 3 condition just check 3 condition in only one if and push it into your result array

Check if one date is between two dates or not

You have interchanged the dates in the if-condition.

Instead of checking, if 2022-02-06 is before 2022-02-07, you are currently checking 2022-02-07 is before 2022-02-06. Which is obviously wrong.

So you have to adjust your check.

For Example:

void main() {
var now = DateTime.now();
var lastYear = DateTime.now().add(Duration(days: -365));

var dateToCheck = DateTime.now().add(Duration(days: -1));

if( dateToCheck.isAfter(lastYear) && dateToCheck.isBefore(now))
{
print("dateToCheck is between now and lastYear");
}
else
{
print("dateToCheck is not between now and lastYear");
}
}

or if you want to write dateToCheck within the function call, you could write it like this:

void main() {
var now = DateTime.now();
var lastYear = DateTime.now().add(Duration(days: -365));

var dateToCheck = DateTime.now().add(Duration(days: -1));

if( lastYear.isBefore(dateToCheck) && now.isAfter(dateToCheck) )
{
print("dateToCheck is between now and lastYear");
}
else
{
print("dateToCheck is not between now and lastYear");
}
}

PHP check if date between two dates

Edit: use <= or >= to count today's date.

This is the right answer for your code. Just use the strtotime() php function.

$paymentDate = date('Y-m-d');
$paymentDate=date('Y-m-d', strtotime($paymentDate));
//echo $paymentDate; // echos today!
$contractDateBegin = date('Y-m-d', strtotime("01/01/2001"));
$contractDateEnd = date('Y-m-d', strtotime("01/01/2012"));

if (($paymentDate >= $contractDateBegin) && ($paymentDate <= $contractDateEnd)){
echo "is between";
}else{
echo "NO GO!";
}

How to determine date falls in between two dates swift

If you have Date objects, they are Comparable and Equatable. You can write code like:

if breakDate >= startDate && breakDate <= endDate {
// breakDate is between startDate and endDate
}

Edit:

As pointed out by LeoDabus in the comments, you could also use the pattern match operator (although I personally don't like it)

if startDate ... endDate ~= breakDate {
// breakDate is between startDate and endDate
}

or contains:

if (startDate...endDate).contains(breakDate) {
// breakDate is between startDate and endDate
}

If you have date strings, create a date formatter that converts your date strings to Date objects, then use code like the above.

If the date strings are always in "Internet" date format (ISO 8601), and you are certain they will always be in the same time zone, you can also use string comparison and compare the date strings directly (using any of the above forms of comparison, except maybe contains?)



Related Topics



Leave a reply



Submit