'JQuery Date.parse function implementation
I am using the daterangepicker component of jquery. The Date.parse() function of jquery gives some weird output when i pass the date as "Jan 2011". Here's the sample output for different use cases:
Code snippet:
var dateStr = "Jan 2011";
var dt = Date.parse(dateStr);
alert(dt.getDate() + "/" + dt.getMonth());
Output :
dateStr = "Jan 2011" : output = 25/0 (todays date is 25 feb 2011)
dateStr = "Feb 2011" : output = 1/1
dateStr = "Dec 2011" : output = 1/11
So for any other month that Jan, the date is set to 1st date of the month, whereas only for jan the date is set to the current date.
Any idea what could be the reason for this?
Solution 1:[1]
It's not standart Date.parse function in you example.
Standart parse function returns numeric result, not Date object.
Javascript parse() Method
In you example parse function returns Date object.
function Date.parse was redefined in you code. Please check it.
Standart parse function always returns first day in month in your case
Solution 2:[2]
function myFunction() {
var d = Date.parse("Jan 2011");
document.getElementById("demo").innerHTML = d;
}
<!DOCTYPE html>
<html>
<body>
<p>Click the button to display milliseconds between a specified date and Jan 2011.</p>
<button onclick="myFunction()">Try it</button>
<p id="demo"></p>
</body>
</html>
The parse() method parses a date string and returns the number of milliseconds between the date string and midnight of Jan 20111
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | |
| Solution 2 | Neeraj Pathak |
