Join the Webstudio community

Updated 4 weeks ago

Conditional Show using dates

Im using Wordpress as my headless, and currently have a custom post named promotions, these have a custom field for the finish date which I can retreive in the Promotion page with no issue.

RIght now in order to show a banner on the page that shows the current promotion is expired we use a taxonomy so they select that the current promo is expired, since I can compare on the expression editor strings I can do this,

However I tried to improve the concept by using the promotion finish date... It should be a simple conditional:

object.finishdate > today ? true : false

However... I need to use at least the javscript Date object but I noticed the expresison editor doesnt allow me to put it inline...
also noticed that the variables we can create can be string, number... but no date

Is there any way I could use Date objects?
O
T
17 comments
good question, with dates its difficult because this also runs serverside, so this would be the date on the server, potentially wrong time zone
mmmmmm good point to consider
I think you may want to check the date on the client and hide/show that
right now im trying in an HTML embed
<script type="module">

const datetoday = new Date();
//id named layer1
//document.querySelector('#layer1').style.display='none';
//document.querySelector('#layer1').style.display='block';
alert($ws$dataSource$7quF08gSQdSqSpMPoDcoA.data.data.promocion.vigenciaPromociones.vigenciafin);

alert(datetoday);
</script>
how can I obtain a variable in a script? or how could I save one from script
cause i could make the compare on the script and save a bool as true or false
or use the queryselector to hide or show
I tried pasting from the expresison builder and got htis $ws$dataSource$7quF08gSQdSqSpMPoDcoA
but i get it as not defined in the console when published
either bind it as an attribute e..g. data attribute or bind the entire html embed value and use a template string as an expression with the script

Plain Text
`<script type="module">const value = ${whatever}`
ok im sure iom being terribly inneficient... but it works...
"<script type=\"module\">\n\n const datetoday = new Date();\n const datepromoend = new Date(\""+$ws$dataSource$7quF08gSQdSqSpMPoDcoA.data.data.promocion.vigenciaPromociones.vigenciafin+"\");\n \nif (datepromoend < datetoday) {\n \ndocument.querySelector('#aftertoday').style.display='none';\ndocument.querySelector('#beforetoday').style.display='block'\n} else {\ndocument.querySelector('#beforetoday').style.display='none';\ndocument.querySelector('#aftertoday').style.display='block'\n \n}\n \n \n alert(datepromoend);\n alert(datetoday);\n</script>"
Im sure i saw about using the variables in text but cant find it so... I used the script as a string begore and after..... ill fix that later
yhanks for the help
to make the code more readable separate the variable from binding into standalone script or use attribute
you are querying that aftertoday element anyways, so you can as well use element.dataset
Add a reply
Sign up and join the conversation on Discord
","upvoteCount":0,"dateCreated":"2024-10-18T23:27:55.080Z","datePublished":"2024-10-18T23:27:55.080Z","dateModified":"2024-10-18T23:27:55.080Z","url":"https://help.webstudio.is/conditional-show-using-dates-FCMzCbh0JMV3#eb1598eb-d477-4edf-96a9-14dd68bb0105","author":{"@type":"Person","url":"https://help.webstudio.is/members/b7fff4f1-2816-4321-b360-36ec6163dc07","name":"Talith","identifier":"b7fff4f1-2816-4321-b360-36ec6163dc07","image":"https://cdn.discordapp.com/avatars/87400188494245888/fac7d111c4875ca5d970eacf6db25f05.webp?size=256"}},{"@type":"Answer","text":"how can I obtain a variable in a script? or how could I save one from script","upvoteCount":0,"dateCreated":"2024-10-18T23:28:17.683Z","datePublished":"2024-10-18T23:28:17.683Z","dateModified":"2024-10-18T23:28:17.683Z","url":"https://help.webstudio.is/conditional-show-using-dates-FCMzCbh0JMV3#d717557c-964d-4c07-8a53-eb9fe16d0fae","author":{"@type":"Person","url":"https://help.webstudio.is/members/b7fff4f1-2816-4321-b360-36ec6163dc07","name":"Talith","identifier":"b7fff4f1-2816-4321-b360-36ec6163dc07","image":"https://cdn.discordapp.com/avatars/87400188494245888/fac7d111c4875ca5d970eacf6db25f05.webp?size=256"}},{"@type":"Answer","text":"cause i could make the compare on the script and save a bool as true or false","upvoteCount":0,"dateCreated":"2024-10-18T23:28:30.974Z","datePublished":"2024-10-18T23:28:30.974Z","dateModified":"2024-10-18T23:28:30.974Z","url":"https://help.webstudio.is/conditional-show-using-dates-FCMzCbh0JMV3#cc2bb9ee-499c-4c20-afe6-20966d281779","author":{"@type":"Person","url":"https://help.webstudio.is/members/b7fff4f1-2816-4321-b360-36ec6163dc07","name":"Talith","identifier":"b7fff4f1-2816-4321-b360-36ec6163dc07","image":"https://cdn.discordapp.com/avatars/87400188494245888/fac7d111c4875ca5d970eacf6db25f05.webp?size=256"}},{"@type":"Answer","text":"or use the queryselector to hide or show","upvoteCount":0,"dateCreated":"2024-10-18T23:28:56.396Z","datePublished":"2024-10-18T23:28:56.396Z","dateModified":"2024-10-18T23:28:56.396Z","url":"https://help.webstudio.is/conditional-show-using-dates-FCMzCbh0JMV3#810b79ec-4d16-4470-ad94-e1a950a14554","author":{"@type":"Person","url":"https://help.webstudio.is/members/b7fff4f1-2816-4321-b360-36ec6163dc07","name":"Talith","identifier":"b7fff4f1-2816-4321-b360-36ec6163dc07","image":"https://cdn.discordapp.com/avatars/87400188494245888/fac7d111c4875ca5d970eacf6db25f05.webp?size=256"}},{"@type":"Answer","text":"I tried pasting from the expresison builder and got htis $ws$dataSource$7quF08gSQdSqSpMPoDcoA","upvoteCount":0,"dateCreated":"2024-10-18T23:29:30.850Z","datePublished":"2024-10-18T23:29:30.850Z","dateModified":"2024-10-18T23:29:30.850Z","url":"https://help.webstudio.is/conditional-show-using-dates-FCMzCbh0JMV3#f7ea75d0-b6df-47ca-867c-6548b6b49d44","author":{"@type":"Person","url":"https://help.webstudio.is/members/b7fff4f1-2816-4321-b360-36ec6163dc07","name":"Talith","identifier":"b7fff4f1-2816-4321-b360-36ec6163dc07","image":"https://cdn.discordapp.com/avatars/87400188494245888/fac7d111c4875ca5d970eacf6db25f05.webp?size=256"}},{"@type":"Answer","text":"but i get it as not defined in the console when published","upvoteCount":0,"dateCreated":"2024-10-18T23:29:52.763Z","datePublished":"2024-10-18T23:29:52.763Z","dateModified":"2024-10-18T23:29:52.763Z","url":"https://help.webstudio.is/conditional-show-using-dates-FCMzCbh0JMV3#4d8f2840-c94a-4b9a-91f7-f0486468b9db","author":{"@type":"Person","url":"https://help.webstudio.is/members/b7fff4f1-2816-4321-b360-36ec6163dc07","name":"Talith","identifier":"b7fff4f1-2816-4321-b360-36ec6163dc07","image":"https://cdn.discordapp.com/avatars/87400188494245888/fac7d111c4875ca5d970eacf6db25f05.webp?size=256"}},{"@type":"Answer","text":"either bind it as an attribute e..g. data attribute or bind the entire html embed value and use a template string as an expression with the script`\"","upvoteCount":0,"dateCreated":"2024-10-18T23:55:25.779Z","datePublished":"2024-10-18T23:55:25.779Z","dateModified":"2024-10-18T23:55:25.779Z","url":"https://help.webstudio.is/conditional-show-using-dates-FCMzCbh0JMV3#d05072cf-e586-402c-b954-f3ab97f2b5fe","author":{"@type":"Person","url":"https://help.webstudio.is/members/b7fff4f1-2816-4321-b360-36ec6163dc07","name":"Talith","identifier":"b7fff4f1-2816-4321-b360-36ec6163dc07","image":"https://cdn.discordapp.com/avatars/87400188494245888/fac7d111c4875ca5d970eacf6db25f05.webp?size=256"}},{"@type":"Answer","text":"Im sure i saw about using the variables in text but cant find it so... I used the script as a string begore and after..... ill fix that later","upvoteCount":0,"dateCreated":"2024-10-18T23:56:27.052Z","datePublished":"2024-10-18T23:56:27.052Z","dateModified":"2024-10-18T23:56:27.052Z","url":"https://help.webstudio.is/conditional-show-using-dates-FCMzCbh0JMV3#935908ad-fd4a-44ce-8825-69bec60f5745","author":{"@type":"Person","url":"https://help.webstudio.is/members/b7fff4f1-2816-4321-b360-36ec6163dc07","name":"Talith","identifier":"b7fff4f1-2816-4321-b360-36ec6163dc07","image":"https://cdn.discordapp.com/avatars/87400188494245888/fac7d111c4875ca5d970eacf6db25f05.webp?size=256"}},{"@type":"Answer","text":"yhanks for the help","upvoteCount":0,"dateCreated":"2024-10-18T23:56:39.045Z","datePublished":"2024-10-18T23:56:39.045Z","dateModified":"2024-10-18T23:56:39.045Z","url":"https://help.webstudio.is/conditional-show-using-dates-FCMzCbh0JMV3#1457dad5-24a8-4e2b-a830-6971f2001447","author":{"@type":"Person","url":"https://help.webstudio.is/members/b7fff4f1-2816-4321-b360-36ec6163dc07","name":"Talith","identifier":"b7fff4f1-2816-4321-b360-36ec6163dc07","image":"https://cdn.discordapp.com/avatars/87400188494245888/fac7d111c4875ca5d970eacf6db25f05.webp?size=256"}},{"@type":"Answer","text":"to make the code more readable separate the variable from binding into standalone script or use attribute","upvoteCount":0,"dateCreated":"2024-10-19T00:20:55.246Z","datePublished":"2024-10-19T00:20:55.246Z","dateModified":"2024-10-19T00:20:55.246Z","url":"https://help.webstudio.is/conditional-show-using-dates-FCMzCbh0JMV3#a2763e5c-d2dc-4b88-b7f3-64dfe36ad941","author":{"@type":"Person","url":"https://help.webstudio.is/members/cdd3ed87-53a0-414a-885a-56b7939e412a","name":"Oleg Isonen","identifier":"cdd3ed87-53a0-414a-885a-56b7939e412a","image":"https://cdn.discordapp.com/avatars/469405813048606720/8b66a5882214c63ee6148fcce3ef8e93.webp?size=256"}},{"@type":"Answer","text":"you are querying that aftertoday element anyways, so you can as well use element.dataset","upvoteCount":0,"dateCreated":"2024-10-19T00:21:45.977Z","datePublished":"2024-10-19T00:21:45.977Z","dateModified":"2024-10-19T00:21:45.977Z","url":"https://help.webstudio.is/conditional-show-using-dates-FCMzCbh0JMV3#13ab52da-806e-4058-aa0f-30f078acdb39","author":{"@type":"Person","url":"https://help.webstudio.is/members/cdd3ed87-53a0-414a-885a-56b7939e412a","name":"Oleg Isonen","identifier":"cdd3ed87-53a0-414a-885a-56b7939e412a","image":"https://cdn.discordapp.com/avatars/469405813048606720/8b66a5882214c63ee6148fcce3ef8e93.webp?size=256"}}]}}