I am creating a date from a string
var fechaStr = "28-11-2017";
var fechaDt = new Date(fechaStr);
But when creating the variable fechaDt
and using it, it throws the message:
variable dateDt undefined
In chrome it works without problems, but when using it in firefox it gives that message.
According to ECMA Script, when you create a new instance of
Date
passing it a value:The following happens:
In your case (2) , since the parameter is a string, we proceed to convert said string into a valid date, using the method
Date.parse (string)
.This method says that it applies very precise rules to do the conversion, and that if there are unrecognizable dates it will return
NaN
(Not a Number). It means that if your date is not in a valid format, it will be a null value:The rules that apply to convert your String into a date are specified in section 15.9.1.15 of ECMA Script:
conclusion
If you are going to construct dates by passing strings to the constructor, they must comply with what is specified in the 15.9.1.15ECMA Script section above.
If you change your date to the format
YYYY-MM-DD
or any other of those indicated, a valid object will be created.For example:
You can't instantiate dates however you want. For example:
either
This format should work in all browsers: