Skip to content

Instantly share code, notes, and snippets.

@Trannosaur
Created August 5, 2020 03:52
Show Gist options
  • Save Trannosaur/8d737430cdd9e62f6a1469a07d732a22 to your computer and use it in GitHub Desktop.
Save Trannosaur/8d737430cdd9e62f6a1469a07d732a22 to your computer and use it in GitHub Desktop.
javascript code to get next day of the week
<html>
<body>
<div id="content"></div>
<script>
const DaysOfTheWeek =
{
SUNDAY: 0,
MONDAY: 1,
TUESDAY: 2,
WEDNESDAY: 3,
THURSDAY: 4,
FRIDAY: 5,
SATURDAY: 6,
}
Object.freeze(DaysOfTheWeek)
function getNextDayOfWeek(sourceDate, targetDayOfWeek, forceToNextWeek = false)
{
var resultDate = new Date(sourceDate) // force a copy so we don't modify the original object
var dayOfWeek = targetDayOfWeek % 7 // fix input if it's too large - ideally we'd throw an exception, but it's not like this is formal code
var dayOffset = (7 /* go to next week */ + dayOfWeek /* go to the day we want */ - sourceDate.getDay() /* minus what we already are */)
if (!forceToNextWeek)
{
dayOffset = dayOffset % 7
}
var dayOfMonthForNextWeekday = resultDate.getDate() + dayOffset
// this is magic, if your specified date goes into next month, it does the maths for us :)
resultDate.setDate(dayOfMonthForNextWeekday);
return resultDate;
}
// TEST CODE, you don't need this:
function printTestResult(testResult)
{
if (testResult) return "✅"
return "❌"
}
function appendText(html)
{
document.getElementById("content").innerHTML += html
}
var startDate = new Date() // today
var firstMonth = startDate.getMonth()
var testResult = ""
appendText("Today's date:<br />" + startDate + "<br />");
// These aren't... super rigorous, but whatever.
var firstSunday = getNextDayOfWeek(startDate, DaysOfTheWeek.SUNDAY)
appendText("<br />This better be a sunday: " + printTestResult(firstSunday.getDay() == DaysOfTheWeek.SUNDAY) +"<br />" + firstSunday + "<br />")
var nextSunday = getNextDayOfWeek(firstSunday, DaysOfTheWeek.SUNDAY)
appendText("<br />Make sure this is the same date as above, because we're not forcing the next week: ")
appendText(printTestResult(firstSunday.getTime() == nextSunday.getTime()) + "<br />" + nextSunday + "<br />");
var forcedNextSunday = getNextDayOfWeek(nextSunday, DaysOfTheWeek.SUNDAY, true)
appendText("<br />Make sure this is the sunday after the week above, because we're forcing the week: ")
appendText(printTestResult(forcedNextSunday > nextSunday));
appendText("<br />" + nextSunday + "<br />");
nextWednesday = getNextDayOfWeek(forcedNextSunday, DaysOfTheWeek.WEDNESDAY, true)
appendText("<br />Make sure this is a Wednesday: ")
appendText(printTestResult(nextWednesday.getDay() == DaysOfTheWeek.WEDNESDAY));
appendText("<br />" + nextWednesday + "<br />")
nextSunday = getNextDayOfWeek(nextWednesday, DaysOfTheWeek.SUNDAY, true)
// guarantee we cross over to the next month:
nextSunday = getNextDayOfWeek(nextSunday, DaysOfTheWeek.SUNDAY, true)
nextSunday = getNextDayOfWeek(nextSunday, DaysOfTheWeek.SUNDAY, true)
appendText("<br />Make sure it crosses over month since first date: " + printTestResult(nextSunday.getMonth != firstMonth) + "<br />" + nextSunday + "<br />");
// Just sanity checking:
appendText("<br />This should not be a tick: " + printTestResult(false))
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment