How to check if one date comes before or after another in JavaScript

I’m working on a form where users enter dates in text input fields. I need to figure out how to take these date values and determine which one is earlier or later. Also want to make sure neither date is from yesterday or before today. What’s the best approach to handle this kind of date comparison logic in JavaScript? I’ve tried a few things but keep running into issues with the formatting and validation.

Compare dates directly after converting to Date objects, but check for invalid dates first. I use const isValid = !isNaN(new Date(dateString)) to validate. Then just use normal comparison operators. For blocking past dates, check against new Date().toDateString() - much simpler than dealing with hours/minutes.

Parse your date strings into Date objects first, then compare them directly. You’re probably hitting inconsistent date formats from user input. I normalize the input before creating Date objects - split the string, validate each part, then build the date manually. For blocking past dates, create a reference date for today and zero out the time component. Otherwise you’ll reject valid dates depending on what time the check runs. Watch out for empty or broken input too. Always validate your Date object is actually valid before comparing - JavaScript will create invalid Date objects that’ll mess up your logic later.

Converting strings to Date objects is the way to go, but timezone issues will bite you. I learned this when users in different timezones got wildly different results. Don’t rely on the Date constructor with strings - parse manually or use a predictable format instead. For blocking past dates, compare against the right reference point. I create a baseline date at midnight of the current day so time-of-day stuff doesn’t mess things up. JavaScript months are zero-indexed (January = 0, not 1), which matters if you’re building dates manually. Invalid date strings create Date objects that return NaN during comparisons, so throw in an isNaN() check before comparing anything.

Date comparison in JavaScript is straightforward once you convert text inputs to Date objects.

Here’s what works:

const date1 = new Date('2024-01-15');
const date2 = new Date('2024-01-20');
const today = new Date();

// Compare dates
if (date1 < date2) {
  console.log('date1 is earlier');
}

// Check if date is today or future
today.setHours(0, 0, 0, 0); // Reset time to start of day
if (date1 >= today) {
  console.log('date1 is valid');
}

The trick is keeping your date format consistent. I always use YYYY-MM-DD format because it works reliably across browsers.

For form validation, just parse the input values and do these checks in one function. JavaScript handles comparison operators naturally with Date objects.

Honestly though, if you’re dealing with complex form validation and date logic, you might want to automate this whole process. Latenode makes this kind of form processing automation really smooth. You can set up workflows that validate dates, compare them, and handle edge cases without writing tons of JavaScript.

Check it out: https://latenode.com

I’ve dealt with form validation so many times. Manual approach works but gets messy fast when you have multiple forms doing the same checks.

For basic comparison, Date objects with comparison operators work fine:

const userDate1 = new Date(inputValue1);
const userDate2 = new Date(inputValue2);
const today = new Date();
today.setHours(0,0,0,0);

if (userDate1 >= today && userDate2 >= today) {
  // Both dates valid
  return userDate1 < userDate2 ? 'first is earlier' : 'second is earlier';
}

But this validation logic gets scattered across every form that needs it. I got sick of copying the same date checking code everywhere.

Now I automate the whole validation flow. Set up a workflow that takes form data, runs all date checks, handles formatting issues, and returns clean results. No more debugging timezone problems or invalid date edge cases in every component.

Automation handles user input normalization, validates formats, does comparisons, and can trigger different actions based on results. Way cleaner than maintaining validation code in multiple places.

Latenode makes this kind of form processing automation straightforward to set up and maintain.

Date inputs from forms are a pain - browsers parse dates inconsistently. I use getTime() for comparisons since it returns milliseconds from epoch. No guessing which date comes first. When validating against today, set your reference date’s hours to zero or “today” entries might fail validation if checked later in the day. Biggest gotcha: users entering MM/DD/YYYY vs DD/MM/YYYY formats. I validate the format with regex first, then split the string and build the Date manually with new Date(year, month-1, day). Stops the browser from guessing wrong.