2

I have following code

startDate = "2012-08-12"

I am subtracting 30 days from that date

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

I want to convert that newDate in the format yyyy MMM dd 2012 JUL 11 (subtracting 30 days from the date)

EDIT

I don't want to use any plugins

I have been trying a long method to extract date, month and year and an array of months then substituting it.

user525146
  • 3,918
  • 14
  • 60
  • 103

1 Answers1

3

You could do this as following:

myDate= Date.parse("2012-08-12") - 2592000000;
newDate = new Date(myDate);

Just parse the date so you get a number

ama2
  • 2,611
  • 3
  • 20
  • 28
  • Umm...wouldn't that subtract 30 milliseconds? – Ed Bayiates Nov 28 '12 at 16:39
  • No, to account for DST. Use UTC to create a new date object ie: Date.UTC("...") see the difference here: http://stackoverflow.com/questions/4110039/javascript-dates-what-is-the-best-way-to-deal-with-daylight-savings-time – ama2 Nov 28 '12 at 16:48