0

I want to get the 2 months back date from today and display it in MM/DD/YYYY format.

Below is my code and it is giving value like this "Tue Feb 11 14:30:42 EST 2014"

   var d = new Date();
                    d.setMonth(d.getMonth() - 2);
                    $('#<%= txtStartDate.ClientID%>').val(d); 
user1144271
  • 35
  • 1
  • 7
  • 1
    [`toLocaleDateString()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/toLocaleDateString) will format it this way in the US locale by default. – tadman Apr 11 '14 at 18:40

2 Answers2

3

Personally I love MomentJS for date manipulation in Javascript. With it you can do this...

moment().subtract(2, 'months').format('MM/DD/YYYY')

and get back this 02/11/2014

It's so easy to read too.

You can download MomentJS here.

nullability
  • 10,545
  • 3
  • 45
  • 63
phuzi
  • 12,078
  • 3
  • 26
  • 50
1
var today = new Date();
  var dd = today.getDate();
  var mm = today.setMonth(today.getMonth() - 2); 
  var yyyy = today.getFullYear();
  today_date = mm + '/' + dd + '/' + yyyy

Try this code

LHH
  • 3,233
  • 1
  • 20
  • 27