3

I have an array of integers that I want to create a new Date with by using new Date but how can i get rid of the brackets so im just passing in the list of numbers? I have this:

var array = [2015, 3, 18, 2, 0, 0]
var newdate = new Date(array) 

but it wont work because new Date cant have an array inside. Does anyone know how to make this work so im passing in:

var newdate = new Date(2015, 3, 18, 2, 0, 0) 

Thanks!

rgc998
  • 299
  • 2
  • 15

1 Answers1

3

spread syntax does the trick.

var array = [2015, 3, 18, 2, 0, 0];
var newdate = new Date(...array);

console.log(newdate);
kind user
  • 40,029
  • 7
  • 67
  • 77