Calculating the Day of the Week from a Given Date

10 May 2020
0 comment
0 attachment

This code snippet demonstrates how to calculate the day of the week from a specific date using mathematical formulas. The result is a number from 1 to 7, where each number corresponds to a day of the week (e.g., 1 for Sunday, 2 for Monday, etc.).


//find the day number with a date
myYear=Year
myMonth=Month
myDay=Day
if myMonth >= 3 then
    D = (((23*myMonth)/9) + myDay + 4 + myYear + (myYear/4) - (myYear/100) + (myYear/400) - 2) mod 7
else
    z = myYear - 1
    D = (((23*myMonth)/9) + myDay + 4 + myYear + (z/4) - (z/100) + (z/400) ) mod 7
endif
return d

The code snippet above calculates the day of the week for a given date specified by the variables myYear, myMonth, and myDay. Here’s a step-by-step explanation of how it works:

  • Variable Initialization: The variables myYear, myMonth, and myDay are set to the current year, month, and day respectively.
  • Conditional Logic: The code checks if the month is March or later (myMonth >= 3). The formula to calculate the day of the week differs depending on this condition.
  • Calculation for March to December: If the month is between March and December, the day of the week is calculated using a specific formula that accounts for leap years and the offset caused by how the Gregorian calendar shifts over time.
  • Calculation for January and February: If the month is January or February, the year is decremented by one (z = myYear – 1) before performing a similar calculation. This adjustment is necessary because January and February are considered the last two months of the previous year for this calculation method.
  • Modulo Operation: The result of the formula is then taken modulo 7 to get a result from 0 to 6, which corresponds to days Sunday through Saturday.
  • Return Statement: The variable d (which should be corrected to D for consistency) is returned, representing the day of the week.

This method is based on a variation of Zeller’s Congruence, a well-known algorithm to calculate the day of the week for any Julian or Gregorian calendar date.

Related Post

Check out this related content for more information:

https://www.prorealcode.com/topic/how-to-avoid-bank-holidays-in-trading-system/#post-75415

Visit Link
What is a Snippet? A snippet is a small, reusable chunk of code designed to solve specific tasks quickly. Think of it as a shortcut that helps you achieve your coding goals without reinventing the wheel. How to Use: Simply copy the snippet and paste it into your project where needed. Don't forget to tweak it to fit your context. Snippets are not just time-savers; they're also learning tools to help you become a more efficient coder.
Nicolas Master
I created ProRealCode because I believe in the power of shared knowledge. I spend my time coding new tools and helping members solve complex problems. If you are stuck on a code or need a fresh perspective on a strategy, I am always willing to help. Welcome to the community!
Author’s Profile

Comments

Search Snippets

Showing some results...
Sorry, no result found!

Snippets Categories

global
35
indicator
133
strategy
171

Recent Snippets

How to Create a Simple MTF Trend Dashboard with EMA and SMA
indicator
This indicator builds a compact multi-timeframe (MTF) dashboard that shows whether price is trading above or below a [...]
How to Display Per-Bar Volume Accumulation in Real Time (Intrabar Updates)
global
This snippet tracks and displays the current bar’s accumulated volume while the bar is still forming, instead of only [...]
Ticks Counter: Count Tick Updates Per Bar on Tick or Time Charts
global
This snippet counts how many tick updates have occurred for the current bar by incrementing a per-bar counter on each [...]
How to Build a Step-Based Trailing Stop That Moves to Break-Even First
strategy
This snippet implements a step trailing stop that advances in fixed increments once price reaches predefined profit [...]
Utilizing Arrays to Track and Compare Indicator Values Within the Same Bar in ProBuilder
indicator
This ProBuilder code snippet demonstrates how to use arrays to compare the values of an indicator (RSI in this case) [...]
Logo Logo
Loading...