Blake Rivell
Blake Rivell

Reputation: 13875

Taking the date from one date and the time from another date and merging into a single date in Typescript

I have four fields that are of type Date in typescript:

StartDate
StartTime
EndDate
EndTime

I need to merge the StartDate with the StartTime into a field called Start and the EndDate with the EndTime into a field called End.

I tried doing it this way and it is not working:

  start: new Date(e.startDate).setTime(e.startTime.getTime()),  
  end: new Date(e.endDate).setTime(e.endTime.getTime()),

Upvotes: 1

Views: 160

Answers (1)

Harry
Harry

Reputation: 5707

Isn't this not enough?

// Prepare mock data
const startDate: Date = new Date();
const startTime: Date = new Date();
const endDate: Date = new Date();
const endTime: Date = new Date();

// Doing merge
const start: Date = new Date(startDate);
start.setHours(startTime.getHours(), startTime.getMinutes(), startTime.getSeconds(), startTime.getMilliseconds());

const end: Date = new Date(endDate);
start.setHours(endTime.getHours(), endTime.getMinutes(), endTime.getSeconds(), endTime.getMilliseconds());

Upvotes: 1

Related Questions