Skip to content

Instantly share code, notes, and snippets.

@ChronSyn
Created April 18, 2020 02:30
Show Gist options
  • Save ChronSyn/91b7e47f745149cbe6f2002956255f49 to your computer and use it in GitHub Desktop.
Save ChronSyn/91b7e47f745149cbe6f2002956255f49 to your computer and use it in GitHub Desktop.
Allows you to take a time from an existing Date object (or date-format string) and set todays' Date object to that time
const adaptTimeForToday = (time: Date | string): Date => {
const existingFormattedDate: Date = new Date(time);
const [hours, minutes, seconds]: number[] = existingFormattedDate.toLocaleTimeString().split(":").map((segment) => Number(segment));
const output: Date = new Date();
output.setHours(hours);
output.setMinutes(minutes);
output.setSeconds(seconds);
return output;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment