0

I'm trying to collect 2 dates using input boxes ids From and To. I would like to subtract 1 day from the date value received using "FROM" text box and pass that new value to the button click event.

The code below works to push values received from the input box. However, i wish to subtract 1 day and pass on the same.

<script src="https://code.jquery.com/jquery-3.4.1.min.js" type="text/javascript"></script>

From : <input type="date" class="form-control" id="From" placeholder="From" aria-label="From">
To : <input type="date" class="form-control" id="To" placeholder="To" aria-label="To">

<input type="button" onclick="location.href='@SITE@/TDL-Test-code.aspx?FromTo=(Date ge datetime%27'+FD+'T00:00:00.000Z%27) and (Date le datetime%27'+ document.getElementById('To').value+'T23:59:00.000Z%27)';" value="Submit" />

Edit: Following is my moment JS function

var oldfrom = document.getElementById('From').value;
newdate = moment(oldfrom).subtract(1, 'days').format("YYYY-MM-DD");
var FD = newdate;
  • 3
    That's a lot of inline javascript. Why not just put that in a javascript ` – Liftoff Feb 24 '21 at 08:35
  • Appreciate the suggestion. I will improve the code as a fix becomes available. Thank you. – Jackson Christopher Feb 24 '21 at 08:40
  • Invalid date error received as i submit the values – Jackson Christopher Feb 24 '21 at 09:28
  • There are many examples here on SO showing how to subtract a day in JS, and plenty of those without needing moment or jQuery, [here's one](https://stackoverflow.com/questions/1296358/how-to-subtract-days-from-a-plain-date). Then to set your `To` value just use `document.getElementById('To').value = ...`. BTW none of your code uses jQuery, why load it? – Don't Panic Feb 24 '21 at 09:43
  • Appreciate the response. This is a small part of the code which needs fixing. There are few parts that require jquery. – Jackson Christopher Feb 24 '21 at 09:59
  • Fair enough. I am just always curious when I see jQuery loaded and then not used. There is a cost in loading it, and IMO mixing plain JS/jQuery code is harder to grok and maintain, but maybe that's just me. – Don't Panic Feb 24 '21 at 12:34

1 Answers1

0

Issue was fixed with solution below.

document.getElementById("From_Local").oninput = function() {
  var dateLocal = document.getElementById("From_Local").value;
  var dateUTC = moment.utc(dateLocal).subtract(4, 'hours').format("YYYY-MM-DD");
  document.getElementById("From_UTC").value = dateUTC;
}
From:<input name="From" required type="date" id="From_Local">
<input type="hidden" id="From_UTC">
To:<input name="To" required type="date" id="To">

<input type="button" onclick="location.href='SITE_URL?FromTo=(Date%20ge%20datetime%27'+ document.getElementById('From_UTC').value+'T00:00:00.000Z%27)%20and%20(Date%20le%20datetime%27'+ document.getElementById('To').value+'T00:00:00.000Z%27)';" value="Submit" />