2

I have two variables here (datetime and time)

And I want to add the time to the datetime variable.

var datetime = '2020/09/21 09:33:00';
var time = '00:00:23';
var result = datetime + time; // I know this is wrong

How can I add the two variables so that the result will be

2020/09/21 09:33:23
Codeblooded Saiyan
  • 1,457
  • 4
  • 28
  • 54

2 Answers2

3
var date = new Date('2020/09/21 9:33');
var time = 23 * 1000;  // in miliseconds
var result = new Date(date.getTime() + time);
Derek Wang
  • 10,098
  • 4
  • 18
  • 39
0

So you need to calculate number of seconds and add it to the date

const datetime = '2020/09/21 00:09:33';
const time = '00:00:23';
const parts = time.split(":");
const seconds = +parts[0] * 3600 + +parts[1] * 60 + +parts[2];
const date = new Date(datetime);
date.setSeconds(date.getSeconds() + seconds);

console.log(date.toLocaleString())
epascarello
  • 204,599
  • 20
  • 195
  • 236