Skip to content

Instantly share code, notes, and snippets.

@yayMark
Created March 22, 2019 02:13
Show Gist options
  • Save yayMark/ec07e4f20da40dd4e04dfd62e74544ee to your computer and use it in GitHub Desktop.
Save yayMark/ec07e4f20da40dd4e04dfd62e74544ee to your computer and use it in GitHub Desktop.
JavaScript: add a day to a date if one time is less than another
const startedTime = '14:30';
const completedTime = '01:15';
const startedDate = '20/01/2019';
if (completedTime < startedTime) {
const [day, month, year] = startedDate.split('/');
const currentDate = new Date(Date.parse(`${year}-${month}-${day}`));
const currentDateUnix = currentDate.valueOf();
const newDateUnix = currentDateUnix + 24*60*60*1000;
const newDate = new Date(newDateUnix);
const newDay = ("0" + newDate.getDate()).slice(-2);
const newMonth = ("0" + (newDate.getMonth() + 1)).slice(-2);
completedDate = `${newDay}/${newMonth}/${newDate.getFullYear()}`;
console.log(completedDate);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment