-1

I want a user to be able to enter a date for new Date object by using prompt. When I initiate a date like this

var dob = new Date("April 2, 1984");

it works, but if I use prompt it becomes invalid.

var urDate = prompt('Date of birth (Ex.:January 3)');
var urYear = prompt('Date of birth (Ex.:1933)');
var dob = new Date(urDate,urYear);
document.write(dob);

What should I do?

Iqbal hossain
  • 1,778
  • 3
  • 17
  • 24
CeeJay
  • 569
  • 2
  • 6
  • 16
  • Please see [*Why does Date.parse give incorrect results?*](https://stackoverflow.com/questions/2587345/why-does-date-parse-give-incorrect-results) You should not use the built–in parser, manually parse strings with your own function or a library. – RobG Aug 28 '17 at 22:23

2 Answers2

0

Looks like this is because your Date function works with a single string parameter while you are using it with 2 parameters the second time.

0

You should concatenate two values urDate and urYear like this:

var urDate = prompt('Date of birth (Ex.:January 3)');
var urYear = prompt('Date of birth (Ex.:1933)');
var dob = new Date(`${urDate} ${urYear}`);
Ivan Minakov
  • 1,432
  • 7
  • 12