0

I'm currently using this to print the current date:

let date = (( new Date()).getMonth()+ 1 ) +  "-" + ( new Date()).getDate() + "-" + ( new Date()).getFullYear()

console.log(date)

I want to be able to print the date fourteen days ago using a similar format. Can this be done using javascript?

A. Ecrubit
  • 561
  • 6
  • 20
joseph
  • 1
  • 1
    Replace `new Date` with a date 14 days ago. – Jonas Wilms May 04 '20 at 19:29
  • 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) – Dr. Acula May 04 '20 at 19:31
  • Is a date 14*24 hours ago ok? (Not the same as as 14 days) – D. Pardal May 04 '20 at 19:31
  • I believe a date 14*24 hours would be ok. It would just need to work when going back to the previous month. Also would be nice if it was in "MM-DD-YYYY" – joseph May 04 '20 at 19:42

1 Answers1

0

You can use Momentjs for that :

let calculatedDate = moment().add(-14, 'day').format("M-DD-YYYY")

document.querySelector('h3').innerHTML = calculatedDate
<script src="https://momentjs.com/downloads/moment-with-locales.js"></script>
<h3></h3>
A. Ecrubit
  • 561
  • 6
  • 20