1

How can I remove 1 week from a date with JS without altering the date format (YYYY-MM-DD).

I saw several exemple but with the current date.

What I tried:

actual = '2017-04-10';
actual.setDate(actual.getDate() - 7);

Thanks.

  • Do what it says here: https://stackoverflow.com/q/10638529/215552 to parse the date, then what it says here: https://stackoverflow.com/q/563406/215552, except add -7. – Heretic Monkey Jun 22 '17 at 18:27
  • Possible duplicate of [Add days to JavaScript Date](https://stackoverflow.com/questions/563406/add-days-to-javascript-date) – Heretic Monkey Jun 22 '17 at 18:27
  • Possible duplicate of [Subtract days from a date in JavaScript](https://stackoverflow.com/questions/1296358/subtract-days-from-a-date-in-javascript) – mhatch Jun 22 '17 at 18:32
  • You're asking 3 separate questions: [*how to parse a date*](https://stackoverflow.com/questions/2587345/why-does-date-parse-give-incorrect-results), [*how to subtract 7 days*](https://stackoverflow.com/questions/1296358/subtract-days-from-a-date-in-javascript), then [*how to format a date*](https://stackoverflow.com/questions/1056728/where-can-i-find-documentation-on-formatting-a-date-in-javascript). – RobG Jun 22 '17 at 20:52

3 Answers3

4

You need to convert your string to a Date first.

Then, to get the format YYYY-MM-DD you can use .toISOString() and keep only the first 10 characters:

var d = new Date('2017-04-10');         // convert string to Date
d.setDate(d.getDate() - 7);             // remove 7 days
var str = d.toISOString().slice(0, 10); // format YYYY-MM-DD
console.log(str);
Aurélien Gasser
  • 3,043
  • 1
  • 20
  • 25
0

The format is dictated by your operating system's regional settings and the format method you call on your date:

// You first need to turn your string into an actual JavaScript date:
actual = new Date('2017-04-10');

// Then you can use the Date API to modify it:
actual.setDate(actual.getDate() - 7);

// But the formatting of the date is determined by your operating system
// regional settings and the Date formatting method you call as well as
// how you, yourself decide to build your own custom format:
console.log(actual);
console.log(actual.toLocaleTimeString());
console.log(actual.toLocaleDateString());
console.log(actual.toISOString());
console.log(actual.getFullYear() + "-" + (actual.getMonth() + 1) + "-" + actual.getDate());
Scott Marcus
  • 64,069
  • 6
  • 49
  • 71
  • 1
    How many times has this question been asked? Do you ever look for duplicates? – Heretic Monkey Jun 22 '17 at 18:27
  • @MikeMcCaughan Yes, I do. But, given that this question combines a few things that none of the links you posted refer to, I chose to give a brief answer. Time to move along Mike. – Scott Marcus Jun 22 '17 at 18:30
  • No, I'll continue to work for the betterment of the site. Thanks for the advice though. – Heretic Monkey Jun 22 '17 at 18:31
  • Also, a question can be marked as duplicate of several questions. – Heretic Monkey Jun 22 '17 at 18:32
  • @MikeMcCaughan Then, you should post links that are relevant. Where is the information on `toLocaleDateTimeString()` or `toLocalTimeString()`? Since this question is about formatting, surely they are relevant. Instead of acting like the the SO police, read my answer and recognize that it adds value to what has not been linked to. Time to move along. – Scott Marcus Jun 22 '17 at 18:35
  • @MikeMcCaughan I'm going to report your comments as harassment Mike. The fact that we disagree on when to post a comment about a duplicate is not something that you should start making accusations about. I believe that when many other sources don't individually answer the question, it's better to leave a new comprehensive answer. I believe that betters the site. You disagree. Fine. Move along. Geeze! – Scott Marcus Jun 22 '17 at 18:43
  • "*The format is dictated by your operating system's regional settings and the format method you call on your date:*" The ECMA-262 *Date* object does not support regionalisation and only one format for parsing. – RobG Jun 22 '17 at 20:48
  • @RobG From MDN: *In older implementations, which ignore the locales and options arguments, the locale used and the form of the string returned are entirely implementation dependent.* And the "implementation" uses the format specified in your local OS. – Scott Marcus Jun 22 '17 at 22:13
  • @ScottMarcus—MDN is not normative, ECMA-262 is (though the quoted statement is correct). The part "*entirely implementation dependent*" means exactly what it says, which is that implementations can return whatever they want. Your statement "*the "implementation" uses the format specified in your local OS*" is demonstrably wrong with minimal testing in current browsers (for me, Chrome 59, IE 10 and Edge return different formats with the date and month in a different order). The statement is also not consistent with ECMA-262 (any edition). – RobG Jun 22 '17 at 22:36
0

Your date needs to be a date object:

actual = new Date('2017-04-10');

actual.setDate(actual.getDate() - 7);
Guillaume
  • 449
  • 3
  • 14