JavaScript Date object time zone, how to set??

When I do:

    var d = new Date();
    console.log(d); // This displays Thu Jun 25 2015 18:41:07 GMT-0700 (Pacific Daylight Time) 

Open in new window


The above displays everything correctly, i.e., the date and time, and even the time zone. Yet when I do:

    var d2 = new Date("2015-06-25"); // Note, this is the SAME date as the above
    console.log(d2); // This displays Wed Jun 24 2015 17:00:00 GMT-0700 (Pacific Daylight Time)

Open in new window


In the second code snippet, why the heck did the date go back one day??

This is messed up! Whoever designed JavaScript this way should be shot in the head! It is clear in the first code snippet that JavaScript knew how to pick up my local time zone. But it can't do that in the second?? All I want is for the Date object to give me the day I asked for, even if it sets the time at 00:00:00. But to give me the previous day is just nuts.

Can someone please tell me how to make these JavaScript dates more predictable??
elepilAsked:
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.

Dave BaldwinFixer of ProblemsCommented:
You can start with the part where the numbers for the different date parts in Javascript are 0 based, not 1 based.  http://www.w3schools.com/js/js_dates.asp   If you had put in a complete text date, it would have worked.

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
elepilAuthor Commented:
Dave, thanks for responding.

You know, I was actually looking for a way to set the time zone, and you nailed it. I was experimenting and learned that I could do:

var d = new Date("2015-06-01 GMT-0700"), and it would work like a charm without any glitches. Thanks for your help.
Dave BaldwinFixer of ProblemsCommented:
You're welcome.  I'm pretty sure that putting 'GMT' in there fixed it for you.  If you just had numbers, it seems that javascript takes it to mean something a little different.  I'm sure they were solving a different problem in 1995 when they first invented javascript.
elepilAuthor Commented:
There's an inconsistency though, and it was glaringly obvious. When I do a new Date(), it knows which time zone. But when I new a new Date("2015-06-25"), all of a sudden it gets confused? Anyway, as long as I can get it to work consistently, that's all that matters.
Dave BaldwinFixer of ProblemsCommented:
If you read thru the link above, you can see that there are several formats.  Date("2015-06-25") does not actually fit any of them.  And if you try Date("2015-06-25 01:01:01") it gives an error.
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.