0

I'm trying to subtract a day from this date format yyyy-mm-dd (string) but can't figure out any way to achieve that using javascript.

What I tried

dateISO = new Date(form.date);
dateISO.setDate(dateISO.getDate() - 1);
Exhaler
  • 105
  • 1
  • 9
  • 2
    Does this answer your question? [Subtract days from a date in JavaScript](https://stackoverflow.com/questions/1296358/subtract-days-from-a-date-in-javascript) – pilchard Nov 30 '20 at 11:55
  • Can you explicitly state what the problem is? What result do you get and what are you expecting? What is the value of `form.date`? – phuzi Nov 30 '20 at 11:57
  • value of ```form.date``` is a string example: 2020-12-04 – Exhaler Nov 30 '20 at 11:59
  • Your code works, even with that string. What is your specific problem? Though you need to declare dateISO: `const dateISO = ...` – pilchard Nov 30 '20 at 12:00

1 Answers1

2

Your code works.

const form = {date: '2020-12-04'}
const dateISO = new Date(form.date);
console.log(dateISO);
dateISO.setDate(dateISO.getDate() - 1);
console.log(dateISO);
pilchard
  • 12,414
  • 5
  • 11
  • 23
  • Since timestamps in the format YYYY-MM-DD are parsed as UTC, it would be better to use UTC methods: `dateISO.setUTCDate(dateISO.getUTCDate() - 1)`. – RobG Nov 30 '20 at 13:41
  • Agreed, I was merely illustrating that the OP's code worked without errors as submitted. – pilchard Nov 30 '20 at 13:55