0

Suppose I've got a date in string format, such as "2021-07-19". How can I subtract x days from this date that is represented as a string?

I have tried to convert the string into a date first, then subtract the number of days, and convert back to a string, but that doesn't work.

const dateString = "2021-07-19"
const previousDayDate = new Date(dateString)
previousDayDate.setDate(previousDayDate.getDate() - 1)
const previousDayDateString = previousDayDate.toString()

The ultimate result should be "2021-07-18". Instead, I get the date as a full string: Sun Jul 18 2021 01:00:00 GMT+0100 (British Summer Time)

Adam
  • 2,384
  • 7
  • 29
  • 66

4 Answers4

1

The reason you get the wrong date is that "2021-07-19" is parsed by built–in parsers as UTC, but all the other methods you're using are local, so you appear to get the wrong date or time. Other than that, your algorithm is sound. Just parse the string as local to being with:

// Parse string in YYYY-MM-DD format as local
function parseISOLocal(s) {
  let [Y, M, D] = s.split(/\W/);
  return new Date(Y, M-1, D);
}


console.log(parseISOLocal('2021-07-20').toString());

This is a very common issue.

RobG
  • 142,382
  • 31
  • 172
  • 209
0

Note, the snippet below didn't work in my locale until I changed the input date to YYYY-MM-DD.

// const dateString = "2021-19-07" - your format
const dateString = "2021-07-19" // format testable in my locale
const previousDayDate = new Date(dateString)
previousDayDate.setDate(previousDayDate.getDate() - 1)
const previousDayDateString = `${previousDayDate.getFullYear()}-${('0' + (previousDayDate.getMonth()+1)).slice(-2)}-${('0' + previousDayDate.getDate()).slice(-2)}`;
console.log(previousDayDateString)
Kinglish
  • 23,358
  • 3
  • 22
  • 43
  • Thanks for your answer. However, I can see the snippet showing the result subtracting 1 month instead of 1 day. In any case, I've done something almost identical in my answer below. – Adam Jul 19 '21 at 14:46
  • 1
    thanks, forgot those were zero-based. Answer updated – Kinglish Jul 19 '21 at 14:48
  • Thanks. However, it still shows as the same date (2021-07-19) – Adam Jul 19 '21 at 14:49
  • Ok. apologies. I accidentally treated the getDate as if it was zero-based, which it isn't. That is fixed. Oddly It gives me the console.log of 2021-07-17 now - but not you? – Kinglish Jul 19 '21 at 14:52
  • It gives me "2021-07-18" which is I think what we want (1 day subtracted from 2021-07-19) – Adam Jul 19 '21 at 14:55
  • Right - that is interesting because it gives me 2021-07-17. Different locales... – Kinglish Jul 19 '21 at 14:57
0

Thank you all for the suggestions. I followed the same convention as Spencer's comment above (How to format a JavaScript date?) by doing:

const dateString = "2021-07-19"
const previousDayDate = new Date(dateString)
previousDayDate.setDate(previousDayDate.getDate() - 1)

const previousDayString = previousDayDate.toLocaleDateString("en-CA").split(",")[0]

console.log(previousDayString)
Adam
  • 2,384
  • 7
  • 29
  • 66
0

Using Moment.js

const dateString = moment("2021-07-19", "YYYY-MM-DD").startOf("day")
const previousDayDateString = dateString.subtract(1, "days").format("YYYY-MM-DD");