0

In my MVC project, I am trying to get the start date value from a cookie which returns a string m/dd/yyyy and I am trying to figure out how to find the number of days that have passed from the string date until now in JavaScript so the function occurs on the client side in the view.

My javascript is incredibly rusty.

currently my cookie value returns string '8/31/2020'. How can I convert this to a javascript date and find how many days have passed?

Any help will be greatly appreciated!

1 Answers1

1

Considering that cookieDate is always in the past, this is the shortest answer:

const cookieDate = new Date('8/31/2020'); const today = new Date();
const diffDays = Math.ceil((today - cookieDate)/(1000*60*60*24)); // diffDays = 22

If you care about daylight saving time check this answer.

nickh
  • 279
  • 3
  • 12