1

Without having to worry about timezone or anything, what's the fastest and best way to -1 day from the input=date on my form within JS, covert to string as YYYY-MM-DD so it can be parsed and used to post to API etc? enter image description here

My date_to var as you can see from my date_to_selector.val() prints the string "YYYY-MM-DD" - I need to minus 1 day from this string in a new variable

Here is my code from the date input "Date To":

<input type="date" name="date_to" />
var date_to_selector         = $(this).parent().find("input[name=date_to]");
var date_to = date_to_selector.val();

I've looked into this and many suggest moment.JS or toISOString but I don't want to have to worry about timezones etc.

Spectric
  • 30,714
  • 6
  • 20
  • 43
JustCode
  • 121
  • 9

1 Answers1

2

You can get the input's valueAsDate property, then set its date to the day of the selected date minus 1:

$('input[type="date"]').change(function(){
  const date = $(this).prop('valueAsDate')
  date.setDate(date.getDate() - 1)
  const res = date.toLocaleDateString('en-US', {year: 'numeric', month: '2-digit', day: '2-digit'}).split('/').join('-');
  console.log(res)
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input type="date" name="date_to" />
Spectric
  • 30,714
  • 6
  • 20
  • 43
  • How can I make the output YYYY-MM-DD string only without T00:00:00.000Z? – JustCode Nov 21 '21 at 03:31
  • This then includes toISOString which means taking into consideration timezones, UTC etc. I've gotten as far as: toLocaleString("en-CA") which outputs into a string correctly however, includes time info too – JustCode Nov 21 '21 at 03:34
  • 1
    @AG9 OK. I've updated my answer again to use `toLocaleString`. – Spectric Nov 21 '21 at 03:38
  • 1
    how bout using moment js its as easy as ```moment().subtract(1, 'days')``` @Spectric – seriously Nov 21 '21 at 03:39
  • @seriously Sounds like a great idea. Not sure if OP wants to use a library though. – Spectric Nov 21 '21 at 03:40
  • without library ```new Date(Date.now() - 8.64e7)``` @Spectric – seriously Nov 21 '21 at 03:46
  • 1
    @Spectric - Perfect, exactly what I needed thanks! with toLocaleString - do I have to worry about timezones etc? – JustCode Nov 21 '21 at 15:02
  • @AG9 As long as you specify the timezone in the first parameter (like I did in my solution, `en-US`), then you shouldn't have to worry about it. – Spectric Nov 21 '21 at 15:42
  • I'm returning the data exactly as needed, but still getting a parsing error "unexpected end of JSON input" - any ideas? – JustCode Nov 21 '21 at 15:59
  • 1
    @AG9 The error probably isn't caused by the date parsing. Looks like you're using `JSON.parse` on something that's not valid JSON. Would you mind sharing the code? – Spectric Nov 21 '21 at 16:01
  • @spectric - is it possible to IM you somewhere to discuss? – JustCode Nov 21 '21 at 16:07
  • @AG9 We can discuss in this SO chatroom: https://chat.stackoverflow.com/rooms/239429/room-for-spectric-and-ag9 – Spectric Nov 21 '21 at 16:09