0

i have a input with type date, I'm getting input value and i need to compare next input value is +1 day or not. I can't figure out how to do that.

I tried:

is my html:

<input type="date" class="form-control" data-model="startDate">
<input type="date" class="form-control" data-model="endDate">

in my js i set to variable my inputs:

var start_dates = $("#periodTable :input[data-model='startDate']");
var end_dates = $("#periodTable :input[data-model='endDate']");

then i get their values:

var end_date = $(end_dates).val();
var start_date = $(start_dates).val();

i get the value of start_date on console:

2018-12-10

and i need to compare the next input value is: 2018-12-11 or not.

here is i tried to get day part of this output with

var start_date_day = start_date.substring(8, 10);

everything is okey i'm getting: 10

if (start_date_day != start_date_day + 1) {

}

but this case not working beacuse i get with start_date_day + 1 = 11

user3348410
  • 2,733
  • 10
  • 51
  • 85
  • Possible duplicate of [Get hours difference between two dates in Moment Js](https://stackoverflow.com/questions/25150570/get-hours-difference-between-two-dates-in-moment-js) – Justinas Dec 07 '18 at 08:11
  • `start_date_day` is a string, if you use the `+` operator on a string, you concatenate it. Use `Number(...)`, `parseint(...)` or `1*` in order to coerce it to a number – Christian Vincenzo Traina Dec 07 '18 at 08:17

2 Answers2

0

Not sure if this is the most efficient way but it should work for you. If the next input value is +1, the compute() function will return true. You can reuse the logic. I have added id attributes to both your input fields. I am also not using jQuery but using native JS, you can convert it to jQuery syntax if you want.

I've created a JSFiddle for the same. https://jsfiddle.net/mazpxcut/

<html>
  <body>
    <input
      id="start_date"
      type="date"
      class="form-control"
      data-model="startDate"
    />
    <input
      id="end_date"
      type="date"
      class="form-control"
      data-model="endDate"
    />
    <button onclick="showVal()">Show</button>
  </body>
  <script>
    function showVal() {
      console.log(compute());
    }

    function compute() {
      var start_date_arr = document
        .getElementById("start_date")
        .value.split("-");
      var end_date_arr = document.getElementById("end_date").value.split("-");
      var start_date = start_date_arr.pop();
      var end_date = end_date_arr.pop();
      if (JSON.stringify(start_date_arr) !== JSON.stringify(end_date_arr))
        return false;
      else return end_date - start_date == 1;
    }
  </script>
</html>
lloydaf
  • 605
  • 3
  • 17
0

Dates in JavaScript work differently, you can refer to the this link for more details You can do something like this:

var end_date = $(end_dates).val();
var start_date = $(start_dates).val();
var ed = new Date(end_date);//this will convert end_date in milliseconds         and store it in ed variable
var sd = new Date(start_date);
sd.setDate(sd.getDate() + 1);//sd now contains a date that is one day ahead of start_date
//now you can check if ed not equal to sd
if(ed != sd){
 //do what u want
}
  • thank you but it's not what i ask exactyly you compare startdate is smaller then end date but i need to compare smaller date -1 day from end date or not. What u did it possible to make with if ( start-date < end_date ) too. – user3348410 Dec 07 '18 at 08:38
  • " i need to compare smaller date -1 day from end date or not". I did not get this part, can you give a little bit more explanation ? – Pavan Gadagi Dec 07 '18 at 09:43
  • eg. i choose start date and it's 01.02.2018 if en date will be 03.02.2018 or any other date i need to give alert like you need to select 02.02.2018 for end date – user3348410 Dec 07 '18 at 09:45