简体   繁体   中英

Validate if date is before date of current date

Using this function, I'm getting a 7 days difference; how can I test whether a date is before the current date?

function validateDate() {
    pickedDate = Date.parse("05-Jul-2012".replace(/-/g, " "));
    todaysDate = new Date();
    todaysDate.setHours(0, 0, 0, 0);
    dateDifference = Math.abs(Number(todaysDate) - pickedDate);
    //7 Days=604800000ms
    if (dateDifference > 604800000) {
        return false;
    } else {
        return true;
    }
}

You can directly compare both dates as

return pickedDate <= todaysDate

For exact date comparison considering milliseconds you can use getTime() method

You can parse date as you have done:

pickedDatestr = "09-Apr-2010"
var pickedDate = new Date(Date.parse(pickedDatestr.replace(/-/g, " ")))

For date comparison (without time):

function isDateBeforeToday(date) {
    return new Date(date.toDateString()) < new Date(new Date().toDateString());
}

isDateBeforeToday(new Date(2016, 11, 16));

Test cases:

// yesterday
isDateBeforeToday(new Date(2018, 12, 20)); // => true

// today
isDateBeforeToday(new Date(2018, 12, 21)); // => false

// tomorrow
isDateBeforeToday(new Date(2018, 12, 22)); // => false

I came to this question because I was trying to check if my Java.util.Date that I was getting from my back-end was after today's date.

In the end I found a really simple solution - by comparing milliseconds, like this:

  isAfterToday(date) {
    return new Date(date).valueOf() > new Date().valueOf();
  }

The function valueOf() is documented here .

new Date() > new Date('2022-01-24')

On my end, I careless about the time.

Try this function

function checkDate(day, month, year)
{
    var regd = new RegExp("^([0-9]{4})-([0-9]{1,2})-([0-9]{1,2})\$");

    var date = month + "/" + day + "/" + year;
    var date = new Date(date);
    var today = new Date();

    var vdob = regd.test(date);

    var err_txt = "" ;

    if(date.getDate() != day || (date.getTime()>today.getTime()))
    {
            err_txt+=("Please select a valid Date.\n")
    }

    return (err_txt);
}

The following will check whether a date occurs before today:

function isBeforeToday(){
  var today = new Date((new Date()).toString().substring(0,15));
  return date < today;
}

This works by creating a new date object after stripping all time information from its corresponding date string:

Tue Mar 06 2018 16:33:15 GMT-0500 (EST) -> Tue Mar 06 2018 -> Tue Mar 06 2018 00:00:00 GMT-0500 (EST)

if(this.dateString1.getFullYear() <= this.dateString2.getFullYear() )//check the year
  { 
    // console.log("date1+date2"+this.dateString1.getFullYear()+this.dateString2.getFullYear())
    if(this.dateString1.getMonth() <= this.dateString2.getMonth())//check the month
      {
        // console.log("date1+date2"+this.dateString1.getMonth()+this.dateString2.getMonth())
        if(this.dateString1.getDate() < this.dateString2.getDate())//check the date
        this.toastr.error("Project Start Date cannot be Previous Date");
          return;
      }
  }

Using this function, I'm getting a 7 days difference; how can I test whether a date is before the current date?

function validateDate() {
    pickedDate = Date.parse("05-Jul-2012".replace(/-/g, " "));
    todaysDate = new Date();
    todaysDate.setHours(0, 0, 0, 0);
    dateDifference = Math.abs(Number(todaysDate) - pickedDate);
    //7 Days=604800000ms
    if (dateDifference > 604800000) {
        return false;
    } else {
        return true;
    }
}

You can directly compare the 2 dates using '<, '>', etc.

function validateDate(date) {
    //get start of day using moment.js
    const now = Moment().startOf('day').toDate();
    if (date < now) {
        return false; //date is before today's date
    } else {
        return true; //date is today or some day forward
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM