-2

I want to get date prior to 30 days from today and prior to 12 months from today. How Do I do that and format as mm/dd/yyyy

This is what I have done to set current date so far:

                var today = new Date();
                var dd = today.getDate();
                var mm = today.getMonth() + 1;
                var yyyy = today.getFullYear();

                if (dd<10)
                dd = '0'+dd;

                if (mm<10)
                mm = '0'+mm;

                today = mm+'/'+dd+'/'+yyyy;
vipworks
  • 13
  • 1
  • 5

1 Answers1

2

You can use setDate and getDate to determine the date 30 days ago.

var date = new Date();
date.setDate(date.getDate() - 30);

Same thing can be done for months with getMonth and setMonth.

Stilltorik
  • 1,662
  • 4
  • 19
  • 31
  • Using *setMonth*, 31 July minus 1 month will be 31 June, which will roll over to 1 July. – RobG Jan 11 '17 at 20:11