Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

JavaScript date does not accept a certain date

I have a problem regarding the JavaScript date function.

var atime = new Date("07.08.2021");
var btime = new Date("19.08.2021");
var ctime = new Date("03.10.2021");
var today = new Date();

console.log(atime);
console.log(btime);

The variables "atime", "ctime" and "today" are working perfectly fine, but btime does not. It always says "Invalid Date", but why, it is completely the same as atime, or ctime?

It does not work with new Date("08/19/2021") either?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Refer to documentation of Date at https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date#several_ways_to_create_a_date_object that warns for parsing strings

Note: Parsing of date strings with the Date constructor (and
Date.parse, they are equivalent) is strongly discouraged due to
browser differences and inconsistencies.

Prefer this kind of notation new Date(2021, 8, 19)

In addition, I would strongly recommend you invest some time to consider using Moment.js, e.g. https://momentjs.com/docs/#/parsing/string-format/

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading