When I apply the following code new Date("25-06-2019") in javascript, it always gives invalid date. But the same format is working in PHP.
What can be the reason of this? not valid format for javascript?
When I apply the following code new Date("25-06-2019") in javascript, it always gives invalid date. But the same format is working in PHP.
What can be the reason of this? not valid format for javascript?
You need to use either an ISO 8601 date, or an RFC 2822 date. See (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date).
If you're doing something more sophisticated, I'd suggest using Moment.js, it will do a lot of Date parsing / formatting / manipulation for you.
For example:
let iso8601Date = '2019-06-25';
console.log(new Date(iso8601Date));
let rfc2822Date = 'Tue, 25 Jun 2019 +0000'
console.log(new Date(rfc2822Date ));
// Or you can use moment.js which is great for date parsing, formatting, etc.
console.log(new moment.utc("25-06-2019", "DD-MM-YYYY"));
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.24.0/moment.min.js"></script>
new Date("25-06-2019")
is giving you invalid date because you are passing invalid date string.
There are 4 ways to create a new date object:
new Date()
new Date(year, month, day, hours, minutes, seconds, milliseconds)
new Date(milliseconds)
new Date(date string)
So your case is 4th one, so valid date string will look like October 13, 2014 11:13:00
If you pass above date string say
console.log(new Date("October 13, 2014 11:13:00"));
It will work fine.
Also, you can provide valid ISO 8601 strings.
console.log(new Date("2008-09-15"));
You can refer http://support.sas.com/documentation/cdl/en/lrdict/64316/HTML/default/viewer.htm#a003169814.htm for ISO 8601 string formats.