Javascript

I typed in the following code:
new Date().getDate()-1;


The answer i got is 16, which is yesterday's date. This is partly what I want. How do i get javascript to give me prior date as 2015-09-16 midnight?

I need the prior date in that format.

Please advise.

Thanks
LVL 1
isamesAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

MlandaTCommented:
var d = new Date();
d.setHours(0,0,0,0);
d.setDate (d.getDate () -1);

d now has  the value you want. You use the setDate function
MlandaTCommented:
Just fixed a typo in there
isamesAuthor Commented:
@MlandaT

Thanks for the query. The result shows as 9/17/2015 12:00:00 AM.

How do I get the date to show as yyyy-mm-dd midnight?
MlandaTCommented:
12:00:00 AM is in fact midnight :)

12AM => midnight
12PM => midday

The variable will ALWAYS store the time portion.

Javascript however does not really deal well with displaying dates in different formats... well... not out of the box... and not in the style of C# where you just say .ToString("dd-MMM-yyyy"). If you want to format the date for display, then you can do some calculations on a date, or (since these problems have been elegantly solved many times already) just use a Javascript Date Library such as moment.js. Then you can do something like;
var d = new Date(); 
d.setHours(0,0,0,0);
d.setDate (d.getDate () -1);

var dm = moment(d);
alert( dm.format('YYYY-MM-DD') );

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
JavaScript

From novice to tech pro — start learning today.