Skip to content

Instantly share code, notes, and snippets.

@yayMark yayMark/addDay.js
Created Mar 22, 2019

Embed
What would you like to do?
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
You can’t perform that action at this time.