Example:
new Date('2020-01-04').getTime() // 1578096000000
new Date('2020-01-04 ').getTime() // 1578124800000
Oddly enough, new Date('2020-01-04 ') gives me the date that I want.
Example:
new Date('2020-01-04').getTime() // 1578096000000
new Date('2020-01-04 ').getTime() // 1578124800000
Oddly enough, new Date('2020-01-04 ') gives me the date that I want.
This behavior is implementation-dependent.
2020-01-04 is a valid date string. Valid formats for date strings can be seen in the specification here. Examples:
YYYY-MM-DDTHH:mm:ss.sssZ
YYYY
YYYY-MM
YYYY-MM-DD
THH:mm
THH:mm:ss
THH:mm:ss.sss
with exactly those characters, and nothing else. Trailing spaces mean that the format is not recognized as valid by the spec, and, per the specification, in such a situation:
The function first attempts to parse the String according to the format described in Date Time String Format (20.4.1.15), including expanded years. If the String does not conform to that format the function may fall back to any implementation-specific heuristics or implementation-specific date formats.
So, it's up to the browser or environment to parse the string if the string doesn't exactly match a required format. No behavior about how the environment parses a non-standard string is required, per the specification; it's undefined behavior.
It seems like you you give that space it is taking that as "2020-01-04 00:00" instead of standard time for 2020-01-04. check the below test. It only works in chrome, in firefox it will give Invalid Date ----- NaN.
var d = new Date("2020-01-04");
document.getElementById("demo1").innerHTML = d + " ----- " + d.getTime();
var d1 = new Date("2020-01-04 ");
document.getElementById("demo2").innerHTML = d1 + " ----- " + d1.getTime();
var d2 = new Date("2020-01-04 00:00");
document.getElementById("demo3").innerHTML = d2 + " ----- " + d2.getTime();
var d3 = new Date("2020-01-04 13:00");
document.getElementById("demo4").innerHTML = d3 + " ----- " + d3.getTime();
<!DOCTYPE html>
<html>
<body>
<p id="demo1"></p>
<p id="demo2"></p>
<p id="demo3"></p>
<p id="demo4"></p>
<script>
</script>
</body>
</html>