0

I am learning date handling and I have a object with start and end date. Ex: Startdate = "2019-12-05" and Enddate = "2020-05-20"

What I want to do is to create a condition that checks if the dates are not empty, and then that the startdate would either be the same as the existing date or a new date that is not less than today's date.

if ((startDate != null && endDate != null) && (????????????????)) {
    alert('Incorrect date');
     this.dateNotValid = false;
}

In place of the ??????? in the code, I want a logic that forces the startdate to be either "2019-12-05" or today's date "2020-03-02" and upward, any other date should flag alert error.

Shravan
  • 907
  • 7
  • 20
Kent
  • 13
  • 4
  • use moment. moment is provide you many pre-define function for check less than, equal and many more to check date – Abhishek Mar 02 '20 at 17:28
  • Are you just having trouble using the `Date` object (assuming `startDate` and `endDate` are of type `Date`)? Please try doing simple comparisons using `Date` objects first. https://stackoverflow.com/a/493018/8109319 – kmui2 Mar 02 '20 at 17:46
  • Can you please clarify "either be the same as the existing date or a new date that is not less than today's date."? What is the "existing date"? – kmui2 Mar 02 '20 at 17:48
  • @stackoverflow-newbie here is the startdate 2019-12-05 in this question – Kent Mar 02 '20 at 17:53
  • I was doing something like this: if ((startDate != null && endDate != null) && (startDate < Startdate || startDate < today'sDate)) but not working. – Kent Mar 02 '20 at 17:57
  • As in if user on the web page want to select date, the date should not be less than this: 2019-12-05. Also the date should not be between this: 2019-12-06 and 2020-03-01. The accepted date should be 2019-12-05 or 2020-03-02->> – Kent Mar 02 '20 at 18:05

1 Answers1

0

I am not sure I understood what you meant by a condition that checks if the dates are not empty, and then that the startdate would either be the same as the existing date or a new date that is not less than today's date.

But I would edit the code this way :

let myInputDate = new Date('any-date-you-want')

let startDate = new Date('2019-12-05'); // sample date
let endDate = new Date('2020-05-20'); // sample date

if ((!!startDate && !!endDate) && ((myInputDate != startDate) && (myInputDate > endDate))) {
    alert('Incorrect date');
    this.dateNotValid = false;
}

As @Abhishek said, consider using moment which is a useful library to play with date objects.

Flo
  • 822
  • 1
  • 7
  • 17
  • See this:: As in if user on the web page want to select date, the date should not be less than this: 2019-12-05. Also the date should not be between this: 2019-12-06 and 2020-03-01. The accepted date should be 2019-12-05 or 2020-03-02->> – Kent Mar 02 '20 at 18:06
  • I got what your logic but where my problem is, is that I don't want user to submit date that is less than 2019-12-05 and any date between 2019-12-05 and 2020-03-02. Valid date should either be existing date 2019-12-05 or date not less that today's date 2020-03-02 – Kent Mar 02 '20 at 18:13