Created
March 22, 2019 02:13
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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