Proposal to add a dayOfWeek() function to Processing

import java.util.Calendar;
import java.util.GregorianCalendar;

int dayOfWeek() { // : 0 to 6, 0 is Sunday
Calendar cal = new GregorianCalendar();
int DAY_OF_WEEK=cal.get(cal.DAY_OF_WEEK);
return DAY_OF_WEEK-1;
}

Hello @EricRogerGarcia,

Thanks for sharing your idea! For Processing feature requests, the best place is to open a GitHub issue so the team can review and track it there.

Raphaël

Hello @EricRogerGarcia,

I have a PDE tab (pde file in sketch) for the utility functions I write and share these between sketches.

You can also create a java file or a jar file for use in other sketches directly in the sketch folder.

I just did the above and it is straightforward with a bit of research.
There are plenty of resources for this out there.

I will leave the exploration of this with you.
The fun part for me is the research and exploration!

Later consider creating a library for use with Processing that can be shared.

Examples:

:)

1 Like

Back in 2018 someone asked how to do this in Java and I suggested they looked at Zeller’s congruence.

It seemed like a fun task so I have implemented a solution and listed the code below.

You can confirm its accuracy here.

Note the function expects the parameters in ‘day-moth-year’ order :+1: :grinning:

/**
 * Calculate the day of the week for any valid date in the 
 * Gregorian Calendar.
 * 
 * author quark 27/8/2025
 */

function setup() {
    createCanvas(400, 400);
    testDayOfWeek(27, 8, 2025);
    testDayOfWeek(8, 5, 1945);
    testDayOfWeek(29, 7, 1969);
    testDayOfWeek(25, "dec", 2025);
    testDayOfWeek(30, 'july', 1966);
    testDayOfWeek(29, 'feb', 2001);
}

function testDayOfWeek(dd, mm, yyyy) {
    let day = dayOfWeek(dd, mm, yyyy);
    console.log(`${dd} / ${mm} / ${yyyy}   Day:  ${day.nbr}   ${day.name}`);
}

/**
Calculates the day of the week
dd    : day of the month 1 - 31  (depends on the month)
mm    : month of year 1- 12 (January - December) or a case insensitive 
        string representing the month or the first few characters of
        the month e.g. 'sept' = September'
yyyy  : the year including the century e.g. 1066

The actual parameters are tested to see if they represent a valid date.

The method returns an object with 2 fields
nbr   : day of week (0 = Saturday ... 6 = Friday) or -1 if parameters do
        not represent a valid date
name  : the full name of the day or "INVALID DATE"
*/
const dayOfWeek = function (dd, mm, yyyy) {
    const months = [
        "january", "febuary", "march", "april", "may,", "june",
        "july", "august", "september", "october", "november", "december"
    ];
    const daysInMonth = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
    const dow = [
        "Saturday", "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday"
    ];
    function isLeapYear(yyyy) {
        let b4 = yyyy % 4 == 0, b100 = yyyy % 100 == 0, b400 = yyyy % 400 == 0;
        return ((b4 & b400) | (b4 & !b100 & !b400)) == 1;
    }
    function getMonthNumber(mm) {
        if (typeof mm === "string") {
            mm = mm.trim().toLowerCase();
            return months.findIndex(m => m.startsWith(mm)) + 1;
        }
        return mm;
    }
    let day = { nbr: -1, name: "INVALID DATE" }
    if (yyyy < 0) return undefined;
    mm = getMonthNumber(mm);
    months[2] = isLeapYear(yyyy) ? 29 : 28;
    if (mm < 1 || mm > 12 || dd > months[mm]) return day;
    mm += mm <= 2 ? 12 : 0;
    let yy = yyyy % 100, cc = floor(yyyy / 100);
    let h = (dd + floor((13 * (mm + 1)) / 5) + yy + floor(yy / 4) +
        floor(cc / 4) - 2 * cc) % 7;
    day.nbr = h; day.name = dow[h];
    return day;
};
2 Likes

https://forum.processing.org/two/discussion/3350/getting-the-day-of-the-week-without-calendar
Another online version but for Java/Pjs:
http://Studio.ProcessingTogether.com/sp/pad/export/ro.9to4yV59zus7B

1 Like

Hello again!

In my research I came across:

  • The java.time package is the modern and recommended library for handling dates and times.
  • The java.util.Calendar and java.util.GregorianCalendar classes are considered legacy APIs and should be avoided in new code.

:)

1 Like

yes, I saw that too but I haven’t yet managed to use the java.time package :upside_down_face:

Thank you for all the answers: I will look into it as soon as possible

(I have a little less time this week!).