Sahand
Sahand

Reputation: 8370

Passing two dates to alert

In javascript, I'm running this:

var y2k = new Date(Date.UTC(2000,0));
var allFives = new Date(Date.UTC(2005,4,5,5,55,55));
alert(y2k, allFives);

I get Sat Jan 01 2000 00:00:00 GMT+0000 (GMT) from alert. I was expecting something like: Sat Jan 01 2000 00:00:00 GMT+0000 (GMT), Thu May 05 2005 05:55:55 GMT+0000 (GMT).

What happens when two dates are passed as arguments to alert?

Upvotes: 0

Views: 45

Answers (3)

imdzeeshan
imdzeeshan

Reputation: 1118

alert() expects only one argument. alert(some expression) you can achieve your output by concatenating variables like -

var y2k = new Date(Date.UTC(2000,0));
var allFives = new Date(Date.UTC(2005,4,5,5,55,55));
alert(y2k + ", " + allFives);

https://developer.mozilla.org/en-US/docs/Web/API/Window/alert

Upvotes: 2

nikitar
nikitar

Reputation: 29

Only one variable should be passed into alert function.

var y2k = new Date(Date.UTC(2000,0));
var allFives = new Date(Date.UTC(2005,4,5,5,55,55));
alert(y2k + ', ' + allFives);

Upvotes: 1

Rohit Agrawal
Rohit Agrawal

Reputation: 1521

alert() assumes this structure:

 alert(some expression)

so you can either convert them to strings and then pass in alert

var y2k = new Date(Date.UTC(2000,0)).toString();
var allFives = new Date(Date.UTC(2005,4,5,5,55,55)).toString();
alert(`${y2k}, ${allFives}`);

Upvotes: 1

Related Questions