-2

I need to subtract months and days from an entry date, but I have a problem with formatting the result to dd/mm/yyyy.

My entry is a string: "dd/mm/yyyy".

mydateEntry = new Date("2021", Number("04") - 1, "01");
myFirstResult = mydateEntry.setDay(mydateEntry.getDay() - (2 * 7));
mySecondResult = mydateEntry.setMonth(mydateEntry.getMonth() - 3);

console.log({ myFirstResult, mySecondResult });

I’m getting the result like this 1617228000000.

I tried to format the results using many functions that I found on the DateJS GitHub repo but it is always not working.

I need to format my results to dd/mm/yyyy.

Sebastian Simon
  • 18,263
  • 7
  • 55
  • 75
sourh
  • 131
  • 2
  • 13
  • There is no `setDay` method. Use `setDate` and `getDate` instead. The processes of [parsing](/q/5619202/4642212) and [formatting](/q/3552461/4642212) dates have been documented countless times. Just use these. – Sebastian Simon Sep 27 '21 at 09:57
  • 1
    @AmirRahman Please read the [documentation](//developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Date/toLocaleDateString). `"ddmmyyyy"` is not a locale. – Sebastian Simon Sep 27 '21 at 09:58

1 Answers1

-1

try this: new Date(mySecondeResult).toLocaleDateString()

Jayendra Sharan
  • 1,043
  • 6
  • 10
  • This does not provide an answer to the question. To critique or request clarification from an author, leave a comment below their post. - [From Review](/review/low-quality-posts/29930941) – nima Sep 27 '21 at 10:16
  • This is the answer for the question. Why do you say it does not provide an answer :/ – Jayendra Sharan Sep 27 '21 at 16:21
  • 1
    @JayendraSharan If you’re wondering, this is a canned comment from review. It’s indeed _the_ answer, but there are two flaws with it: 1. [“Try this” answers are not useful](//meta.stackoverflow.com/a/298811/4642212). Please add some explanation. Code-only answers are less useful for future readers and don’t explain the OP’s mistake or how to approach the problem. 2. This is the [25th most frequently asked JavaScript question](/questions/tagged/javascript?tab=Frequent). It has been answered _a lot_ already, and unfortunately this answer doesn’t add anything new. – Sebastian Simon Sep 27 '21 at 19:16