Weekday function

Is there any way in Wappler to return the day of the week of a date? I’m thinking of something like Excel’s WEEKDAY() function.
I looked in the formatter’s Date and Time menu and could not find anything.

Many thanks in advance,

Alex

using myvalue.formatDate(“dddd”) will return Monday - Sunday as text

1 Like

THank you Brian,

Is there something that returns a number from 0 to 6 or 1 to 7 for the day of the week of a date? I need to do some calculation based on the % of the week elapsed on a certain date. I guess I could find the day of the week using a really long ternary operation, but a function would be nicer :slight_smile:

Many thanks,

I think I found how to do it!

In case someone else needs to do this, it can be done by inserting a date picker, and selecting its Format as a simple d.

image

The date picker then returns the day of the week from 0 to 6 (0=Sunday, 1=Monday, and so on).

Alex

1 Like

Hmmm. I stand corrected.
Yes the approach I described above “shows” the right number, but the content of the variable is still the full date. I really need something that returns that number so I can do date math, adding it to something else.

Back to the grinding stone :frowning:

This solution is going to hurt your eyes, hahaha, but it would work

<!-- Standard Wappler Date Time component to get todays date and time, with interval set to days so it ignores the time portion -->
<dmx-datetime id="varToday" interval="days"></dmx-datetime>

<!-- Standard Wappler Variable to Format the date TODAY to Monday, Tuesday... then replace names with numbers, then convert returned number to an integer -->
<dmx-value id="varTodayFormatted" dmx-bind:value="varToday.datetime.formatDate('dddd').replace('Monday', 1).replace('Tuesday', 2).replace('Wednesday', 3).replace('Thursday', 4).replace('Friday', 5).replace('Saturday', 6).replace('Sunday', 7).toNumber()"></dmx-value>

<!-- Standard Wappler Variable to add 1 day to today and Format the date TOMORROW to Monday, Tuesday... then replace names with numbers, then convert returned number to an integer -->
<dmx-value id="varTomorrowFormatted" dmx-bind:value="varToday.datetime.addDays(1).formatDate('dddd').replace('Monday', 1).replace('Tuesday', 2).replace('Wednesday', 3).replace('Thursday', 4).replace('Friday', 5).replace('Saturday', 6).replace('Sunday', 7).toNumber()"></dmx-value>

<!-- Call the two variable integers and add the 2 numbers together, Friday + Saturday is 5+6=11 -->
{{varTodayFormatted.value + varTomorrowFormatted.value}}

Obviously I added much more to this so I could ensure the number parsed back was converted from a string to a number so you could perform mathematical operations on it.

Good Luck.

You could of course simplify that slightly i.e.
var1.datetime.getDay() :laughing:

2 Likes

Mwahahahha, I had no clue you could get that out of there, lol, thanks Brian, your solution is just slightly faster than mine.

1 Like

I know this is already resolved, but because I can not believe how i missed that I decided to double check on my side, and I do not even have a formatter for Get Day, I assume your list has one, but mine doesnt, and i can not scroll down more to find any others, mine literally ends on Get Month.
So very excited, maybe I found a bug on the Mac version and when resolved maybe i will have loads more options that what I already have.
Can you show me a screenshot of yours Brian.
@brad you are on mac, what do you have available?

Well I just installed Wappler on my PC and that also has no formatter for Get Day, just like the mac it stops on Get Month.
Also @Hyperbytes, how did you know you could take all the parameters out of the Get Day like you did, in the formatters it will not let you go anywhere until you have entered a value?

Hi Paul

It is not in the formatter list but you can manually add pretty much any javascript function manually which is what I did

Perfect, thanks Brian, that opens up a whole new world to me, mwahahaha, I know what research i’m doing today.

I think it’s there - but hiding. This is on a Mac, but it’s the same on Windows:

image

The down arrow key will navigate to the otherwise unfathomable bottom of the list. The javascript function it creates is getDate(), but it’s easier to enter it manually - for the reason you suggest regarding the formatter requiring you to add extra parameters.

There are a few more related javascript functions, but they don’t seem to be implemented in Wappler (they’re missing from dmxFormatter.js).

2 Likes

Thanks Tom, that helped, mine are now there with the down arrow, great tip, thank you.

Never even noticed those lists scrolled with down arrow, i just guessed that was the end of the list! Nice tip

The getDay() method is exactly what I needed.
It works like a charm and I can perform math operations on the resulting number.

That said, there seems to be 2 small bugs in the Formatter:

  1. The first one is in the Date and Time operations menu. As TomD pointed out, the Get Day of Month and Get Day of Week options are available, but one cannot reach them with the mouse but first putting the mouse on a menu option (say Get Month) and then hitting the down arrow To the risk of sounding obvious, I’d say that this is not the best UI: one s/b able to access the options w/o this bit of info.

  2. Once the Get Day of the Week is selected, the interaction with the formatter is a bit confusing because it prompts you for a Date. This looks odd to me, because the GetDate() or the GetDay() methods are methods of the already supplied date, the one that is at the top. And this Date, that looks like a parameter to the method(s) is a mandatory field, so you have to fill it in with something to be able to click the Select button and then, I imagine, edit it out.

I don’t want to sound like a nit picker because I’m very happy that the method exists and that I know how to use it. But I’m sure that these two bugs will appear in other conversations if not fixed.

Thank you everyone!

Alex

I get what you mean

Only var1.datetime.getDay() is needed

the formatter insists on

var1.datetime.getDay(var1.datetime)

Not sure if it is a bug or intended for some reasons of flexibility

Officially getDay() takes no parameters