Looking for a way to determine the weekday of any date

What i am looking for is - so to speak - another version of the current block.
I would like to give it a date - let's say the first of january of any year and by that determine the weekday. This helps me to determine if a year has 52 or 53 weeks.

Other than writing my own javascript function to achieve this I could not come up with.

Perhaps there is a simple way I am overlooking?

Thx for an answer

Regards Frank

If you know an algorithm that would work in Javascript, you can code the same algorithm in Snap! and it'll work fine!

Of course your algorithm has to know the year number in addition to knowing the day of the week for Jan 1, because of leap years.

No because in javascript you can do

const d = new Date(timestamp);
console.log(d.getDay()) // returns day of week not month or year

You can't do that in Snap however

exactly the reason why i want to revert to a javascript function.


I don't know of any shortcut in Snap for this other than a javascript function.

I have no experience with JS functions in Snap so I hope this is the correct way to implement this

I have a couple of datetime blocks that might be useful for your needs

The OP said "writing my own javascript function." I took that to mean actually implementing the date processing, not just using a library.

Look, when you want to know something about the computer's hardware, such as where the mouse is, you have to interface with the browser, and the way provided to do that is Javascript. (In the case of where the mouse is, we've already provided access to the needed Javascript code, in the form of a Snap! primitive.)

But day/date conversions aren't like that. Javascript doesn't have any magic access to the rules about which months have how many days and so on. That JS library just does plain old computation, and the same algorithm it uses will work in any language. (Specifically, @cymplecy's blocks don't use JS Function -- read his code!)

The algorithm to find the day of the week from a month-day-year date is a little messy, but not so terrible.

It seems to be The XY Problem around leap years.

A couple of the hidden helper blocks might be useful

Hi - are your solutions available somewhere? Would love to take a peek.

TIA
Frank

drag

into the snap editor, I think

Date() is a native javascript constructor, not part of a library

Okay, fine, so it's a built-in library. :~) My point is, it just does regular old computation on numbers, nothing hardware-specific.

You can drag and drop script pics (the images of snap script) into snap, and you'll get the scripts in the image. If it doesn't work, click on the image to make it bigger, then drag and drop it.

Off-topic:
This also works for MicroBlocks and App Inventor generated png files.

Wait, did they steal our idea or did we steal theirs? I was pretty sure we invented it. (Just curious, of course it's great when people use each others' ideas.)

Brian Silverman invented storing program code in pictures generated by TurtleArt, where every project is stored - only - as PNG. He generously shared the idea with us, John first implemented it in MicroBlocks and then @dardoro helped us bringing it to Snap!. If AppInventor now also has it that's a great example of a good idea catching on!

@jens App Inventor has it since 2015.

Nope! It just opens the sprite. ??

You will need to click on the image to get the full-size version before you can drop it into Snap! and get the script.