Neither of those formats is necessarily supported by the Date
object. The only format it's required to support is the ISO-8601 variant described by the specification.
Use the multi-argument constructor (note: 6 = July, the months start at 0):
// To get a Date instance
new Date(2018, 6, 26); // Local time
new Date(Date.UTC(2018, 6, 26)); // UTC
// To get the milliseconds-since-the-Epoch value
new Date(2018, 6, 26).getTime(); // Local
Date.UTC(2018, 6, 26); // UTC
Most (all?) JavaScript engines will also accept a U.S.-style date (even in non-U.S. locales) in the form mm/dd/yyyy
, but you can't be sure whether it will parse that in local time or UTC. That's why your first example works (sort of, the result is January 7th, not July 1st), and your second doesn't (the "month" isn't valid).
If you don't want to handle the parsing yourself, there are libraries like Moment that do it (and a lot of other things) for you.