0

In my JavaScript, I have a calendar. The selection will look something like "04-01-2020". I have a function that checks if the from date and to date are over 30 days. How do I get the date 30 days from that date? I'm guessing I'd have to convert the "mm-dd-yyyy" to javascript's date format before converting back somehow.

BobbKitty729
  • 327
  • 1
  • 6
  • 13

1 Answers1

0

You can try this

var oldDate = new Date(new Date().setDate(new Date('2020-06-17').getDate() - 30));

console.log((oldDate.getMonth()+1)+"-"+oldDate.getDate()+"-"+oldDate.getFullYear());
Ashik Paul
  • 486
  • 4
  • 20
  • Is there a way to get ```oldDate``` to be the format "mm-dd-yyyy" only? When I run it now, it gives me ```5/17/2020, 2:17:22 PM``` – BobbKitty729 Jun 17 '20 at 18:18
  • I have changed the format. Is this what you expected? – Ashik Paul Jun 17 '20 at 18:41
  • '2020-06-17' will be parsed as UTC, the other methods are all local so the result will be 1 day off depending on the time of day that the code is executed and the local timezone offset. – RobG Jun 17 '20 at 20:23
  • @AshikPaul For some reason, it will always be starting on May or ```5```, even if I change ```new Date('2020-06-17').getDate()``` to ```new Date('2020-02-17').getDate()```. Do you know why this is? – BobbKitty729 Jun 17 '20 at 22:41