0

I just started learning Javascript and currently, I am following the Javascript Crash course for Beginners by Traversy Media on Youtube. (https://www.youtube.com/watch?v=hdI2bqOjy3c - I am at 1:02:00)

I am following his example with Constructor Function, but I get a different result on the console from the one he gets and (probably because I am just a beginner) I don't understand why.

The code is:

function Person(firstName, surname, dob) {
  this.firsName = firstName;
  this.surname = surname;
  this.dob = new Date(dob);
};

const person1 = new Person('Mike', 'Smith', '5-6-1990');
const person2 = new Person('Lisa', 'Gregory', '3-6-1970');

console.log(person2.dob);

now, why do I get Invalid Date while he gets Fri Mar 06 1970 00:00:00 GMT-0500 (Eastern Standard Time) ?

Am I doing something wrong?

enter image description here

RobG
  • 142,382
  • 31
  • 172
  • 209
Erikamyself
  • 263
  • 1
  • 13
  • I just ran the code in your question. Works fine for me. – Yousaf Aug 28 '20 at 15:29
  • seems to work in snippet – M Z Aug 28 '20 at 15:29
  • I am so annoyed, what could that be? I can only think about my location, I am based in Europe and while in America 3-6-1970 means 6th of March 1970, here means 3rd of June 1970. Could that be part of the problem? – Erikamyself Aug 28 '20 at 15:34
  • I'd rather put the money on your browser (version) instead of the locale and timezone, but yes, don't use `new Date` with custom formats. – Bergi Aug 28 '20 at 16:16
  • I've tried with Safari Technology Preview 14.0 and Safari 13.1.2 ... they both give me the same result... – Erikamyself Aug 28 '20 at 16:47
  • Right, It works with Chrome... – Erikamyself Aug 28 '20 at 16:55
  • 1
    "5-6-1990" is not a format supported by ECMAScript so parsing is implementation dependent. Don't use the built–in parser. – RobG Aug 28 '20 at 20:17
  • Are you sure that it doesn't work? I put that on the console and it printed `Fri Mar 06 1970 00:00:00 GMT+0100 (hora del meridiano de Greenwich)` – Ana Santana Aug 28 '20 at 15:30

0 Answers0