1

I have input in html like this:

<input class="form-control" placeholder="Date of Collection *" id="m_date" name="m_date" type="date" tabindex="6" required/>

I would like to select a date that is more than 7 days from the current date, if I select a date before 7 days from current, it should prompt saying "Wrong date selected"

How do I do that in javascript?

I tried the following:

var date = new Date();
date.setDate(date.getDate() + 7);

console.log(date);

It gives the date correctly. How do I use this to compare if date is 7 after or not and prompt accordingly?

Thanks!

UPDATE:

<html>

<body>

<input class="form-control" placeholder="Date of Collection *" id="m_date" name="m_date" type="date" tabindex="6" required/>

</body>

</html>

<script>

let cal = document.body.getElementsByClassName('form-control')[0];
cal.onchange = function(e) 
{
    var selectDate = e.target.value
    var startDate = new Date(Date.parse(selectDate));
    console.log(startDate);
    
    var dateAfter7Days = new Date(new Date().getTime()+(7*24*60*60*1000))
    
    console.log("7 days " + dateAfter7Days);

    if (startDate => dateAfter7Days )
    {
        console.log("Allow");
    }
    else
    {
        console.log("Don't allow");
    }
}

</script>

I am getting "Allow" for any date I select.

Sanjana Nair
  • 2,663
  • 6
  • 26
  • 44
  • Does this answer your question? [How do I get the number of days between two dates in JavaScript?](https://stackoverflow.com/questions/542938/how-do-i-get-the-number-of-days-between-two-dates-in-javascript) – Nora Apr 27 '21 at 02:12
  • If you do what you've did in your example, you can just compare the selected date and make sure that it is not bigger than this `date` object. – Titus Apr 27 '21 at 02:19
  • I don't know how to compare dates by the given date and prompt immediately wrong date. – Sanjana Nair Apr 27 '21 at 02:20
  • So you want to get 'Wrong date selected' if a date not in coverage is selected? – jacobkim Apr 27 '21 at 02:22
  • You can compare the date's time, something like this `date.getTime() <= selectedDate.getTime()`. If this condition is true, the selected date is not more than 7 days in the future. – Titus Apr 27 '21 at 02:23
  • @jacobkim that is right – Sanjana Nair Apr 27 '21 at 02:23
  • @Titus: Please check my update, can you help me? – Sanjana Nair Apr 27 '21 at 03:05

3 Answers3

1
var date = new Date();
var next_seven_date = d.getDate()+7;
var current_month = d.getMonth();
current_month++; // month start from 0 then we need to +1
var current_year = d.getFullYear();

var weekDate =(next_seven_date + "/" + current_month + "/" + current_year);

date.setDate(weekDate);
Tony Tin Nguyen
  • 170
  • 1
  • 7
1

The point is comparing two date values. If current date - selected date > 7 then it should print prompt. The problem is how to get selected date.

You can get the selected date from the input tag by event value. On changed date, the value get logged.

let cal = document.body.getElementsByClassName('form-control')[0];
cal.onchange = function(e) {
console.log(e.target.value);
}
<input class="form-control" placeholder="Date of Collection *" id="m_date" name="m_date" type="date" tabindex="6" required/>
jacobkim
  • 1,043
  • 10
  • 20
  • I tried the following but didn't work let cal = document.body.getElementsByClassName('form-control')[0]; cal.onchange = function(e) { console.log(e.target.value); var selectDate = e.target.value var date = new Date(); date.setDate(date.getDate() + 7); console.log(date); if (selectDate => date) { console.log("Allow"); } else { console.log("Don't allow"); } } – Sanjana Nair Apr 27 '21 at 02:45
  • @SanjanaNair Seems my explaining isn't good! On if condition the condition should be value of current date - value of selected date >= value of 7 days. – jacobkim Apr 27 '21 at 06:32
  • @SanjanaNair the values of dates should be set of data as number of milliseconds. – jacobkim Apr 27 '21 at 06:34
1

Since your problem is to compare dates not creating them I have updated my answer which might hlp you

var currentDate= new Date();
currentDate= new Date(currentDate.getFullYear(),currentDate.getMonth(),currentDate.getDate(),0,0,0)

var idealDifference= (7*24*60*60*1000);

//In your case this date might comes from some date selection user control. Be aware to make the time part of each date to same
var userSelectedDate = new Date(2021, 04, 04,currentDate.getHours(),0,0,0) 

if((userSelectedDate.getTime()-currentDate.getTime())>=idealDifference)
{
    console.log(userSelectedDate, ' is after 7 days from ',currentDate)
}
else
{
    console.log(userSelectedDate, ' is before 7 days from ',currentDate)
}

Note: It is important to unset the time part of both the dates before comparing for this logic to work

Beingnin
  • 2,288
  • 1
  • 21
  • 37
  • see the updated answer. Since I am using epoch time for comparison it wont be that accurate to seconds level. but this might be enough for most cases – Beingnin Apr 27 '21 at 03:14