We are about to switch to a new forum software. Until then we have removed the registration on this forum.
I've written a program that needs to determine the day of the week to operate. Before, I was using Calendar and Gregorian Calendar, which work great.
However, I need to host the program online, so to my knowledge I can only use the day() function because Java Imports don't work online.
Is there a way to easily calculate the day of the week (from 1 - 7 etc) in a way that will work with JavaScript online?
Thank you!
Answers
In JS, it's as simple as to type in ->
var week = new Date().getDay();
in order to obtain the # of the current week! O:-)However by doing so, it breaks Java Mode! And you'd be stuck in JS Mode from that moment on! :o3
A proper solution to keep both Java & JS modes working is to create a independent ".js" tab.
Then setup an interface from the ".pde" side to call upon that native side!
That's exactly I was reading about these days. If you're interested, read on that article below:
processingjs.org/articles/PomaxGuide.html
But you're very luck this time! Java got a class called Date too! And coincidentally, it got a method called getDay() as well!
download.java.net/jdk8/docs/api/java/util/Date.html#getDay--
Even though deprecated, it works alright! :D
Hence Date class functions as a compatibility bridge for such Java & JS methods! <:-P
Even w/ this fix easily possible, Processing should really consider providing a week() along the other Date functions as well! :-w
If you want a version that independent of any Java libraries then try this (based on Zeller's algorithm)
Fantastic @quark ! But beware of reassigning Processing APIs like day() due to JS:
String[] day = {};
-> day()@GoToLoop - good point have changed the code above to avoid clash with day()
Nice @quark ! Although a plural name would be even better! I know, I'm being grammar Nazi! >:)
@dangreenbergdesign already had a thread where his JS was bugged exactly due to a String reassigning day()! @-)
forum.processing.org/two/discussion/3132/cant-figure-out-why-processing-java-code-not-working-with-javascript
That would be personal preference and I have used both singular and plural in the past and will in the future.
dayNames
- describes the entire array but when referring it its elements we are talking about a single name i.e.dayName[3]
so take your pick.I don't think there is an accepted naming convention for arrays and collections that covers this. :-?
Officially that's true! /:)
I consider dayNames[3] as picking element #3 amidst the dayNames! :P
Thanks for your help! You're right, I did have some issues with day vs day() earlier haha.
Which would be the most robust way to do this? I don't mind breaking regular Java, just need it to work online.
I tried adding in the codes, but none seemed to work with java. My code is below, could you please show a noob how to incorporate it?
Thanks!!!
Since your are only interested in the current date and time then there is nothing incorrect with your code. The example I gave was to find the day-of-the-week for any arbitrary date so is not applicable here.
Since the clock resolution is 1 second then you only need a frame rate of 1 fps, unless there are other things to add that need a higher framerate.
Both my
new Date().getDay();
snippet and @quark 's utility function work in both Java & JS modes! :-wBoth of them return an
int
in a range of 0 to 6 representing the day of the week! \m/So I don't get why you couldn't apply any of those solutions into your program! :>
Anyways, I've made a static class which represents a FullDate, from second up to year, and including the week too! :-bd
And in setup() there are some examples of its usage.
Also it's working in both Java & JS of course. You can check it out running online below: B-)
studio.processingtogether.com/sp/pad/export/ro.9to4yV59zus7B/latest
Good luck adapting it to your code: %%-
I got Quarks code to work, was just misreading how to implement it :/
Thanks for all your help! :-c