0

I was trying to make a javascript calender function as like in Google Analytics. to display last week calander.

But couldn't figure it out for example.

var d = new Date();
d.setTime(d.getTime() - (d.getDay() ? d.getDay() : 7) * 24 * 60 * 60 * 1000);
d.setTime(d.getTime() - 7 * 24 * 60 * 60 * 1000);               
alert(d);

Result Sun Aug 24 2014 02:46:57 GMT+0545

but i want result in this format 08/24/2014

Shurya
  • 7
  • 2
  • The one with the RegEx option `(new Date()).toISOString().replace(/^(\d{4})-(\d{2})-(\d{2}).*/,"$2\/$3\/$1")` – Dalorzo Sep 03 '14 at 21:14
  • 1
    Not trying to be a bad person but how was this question upvoted? A google search on "Javascript date" returns more hits than one could thoroughly read through during a workday. Wasn't a Google Search plus rolling up sleeves by the OP the solution here? – The One and Only ChemistryBlob Sep 03 '14 at 21:25
  • You can use `alert(new Date().toLocaleDateString());` This will give you the format you want and none US user the date in a format they understand. If you want it exactly in the format you say you can use toLocaleDateString with some options `alert(new Date().toLocaleDateString("en-US", {year: "numeric", month: "2-digit", day: "2-digit"})); – Richard Houltz Sep 03 '14 at 21:42

1 Answers1

1

You should be able to use the formatting methods of Date, like this:

alert(d.getMonth() + '/' + d.getDate() + '/' d.getFullYear())

Edit: Months are 0-based I believe, so should be like this instead (left previous version for reference):

alert((d.getMonth() + 1) + '/' + d.getDate() + '/' d.getFullYear())
SharkofMirkwood
  • 11,483
  • 2
  • 17
  • 25