• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
JavaScriptSource

JavaScriptSource

Search 5,000+ Free JavaScript Snippets

  • Home
  • Forum
  • Browse Snippets
    • Addon
    • Ajax
    • Buttons
    • Cookies
    • CSS
    • Featured
    • Forms
    • Games
    • Generators
    • Image Effects
    • Math Related
    • Miscellaneous
    • Multimedia
    • Navigation
    • Page Details
    • Passwords
    • Text Effects
    • Time & Date
    • User Details
Home / Time & Date / Get the date for a specific day of the week

Get the date for a specific day of the week

Here’s a way to get the date for the next occurrence of a specific day of the week.

/*!
 * Get the date for a specific day of the week
 * (c) 2021 Chris Ferdinandi, MIT License, https://gomakethings.com
 * @param  {String} dayName The day of the week (case-insensitive)
 * @return {Date}           The date object
 */
function getNextDay  (dayName) {

	// The current day
	let date = new Date();
	let now = date.getDay();

	// Days of the week
	let days = ['sunday', 'monday', 'tuesday', 'wednesday', 'thursday', 'friday', 'saturday'];

	// The index for the day you want
	let day = days.indexOf(dayName.toLowerCase());

	// Find the difference between the current day and the one you want
	// If it's the same day as today (or a negative number), jump to the next week
	let diff = day - now;
	diff = diff < 1 ? 7 + diff : diff;

	// Get the timestamp for the desired day
	let nextDayTimestamp = date.getTime() + (1000 * 60 * 60 * 24 * diff);

	// Get the next day
	return new Date(nextDayTimestamp);

}

Now, you can do something like this:

var nextMonday = getNextDay('Monday');

// You can use lowercase, too
var nextSaturday = getNextDay('saturday');

// You can even do weird mixed case
var nextFriday = getNextDay('friDay');

Source

https://vanillajstoolkit.com/helpers/getnextday/

Time & Date

Related Snippets:

  • Check if a date is between two other dates
  • Event Calendar Code Generator
  • Check if a date is after another date
  • Check if a date is before another date

Primary Sidebar

FREE UPDATES!

Get the latest updates in your inbox for FREE!

Popular Posts

Story Generator

IP Grabber

Simple Calendar

Remove Ads

Astrological Calculator

Copyright © 2022 JavaScriptSource.com

  • About
  • Privacy Policy
  • Submit
  • FAQ
  • Jobs For Developers
  • Contact Us