Retrieve Today's Date

How do I capture the current date with Wix Code? (I need to subtract a number from the current date, but to do that, I need to know how to retrieve the current date first.)

Hi!

This is pure javascript. JavaScript Date Objects

Just do this:

let d = new Date()

And then you can modify it the way you want
Wixcode is framwork, which has Javascript as an underlay, so you can use almost any function from javascript in code

how did you subtract day from current date?

I find it very frustrating speaking to anyone from Wix. They seem condescending to us “know nothings”
If you guys feel we don’t know anything then don’t bother to respond. Rather tell us to do an entire javascript course. I have struggled with wix code for over 6 months now and I am NOT getting it.
Im trying to get a field to equal today’s date when the page opens. Don’t have a clue how…But I will keep trying. You guys need to realise not everyone knows javascript like you!

2 Likes

See if this works.

const today = new Date();

const options = {
    day: "numeric",
    month: "long",
    year: "numeric"
};
 // Sets the property of the text element to be a string representing today's date in US English
$w("#yourElementname").text = today.toLocaleDateString("en-US", options);



See if ‘what’ works???
Where does this go?
What heading is needed?

Hi Wayne, sorry you’re finding this frustrating. It gets easier as you go along. See if the info below helps.

  1. Start in the Wix Editor, and from the Code menu item, turn on developer tools. I know you know that already, but maybe someone else will read this.
  2. Add the element in which you want to display the date. I added a text box, which has the default name #text1. It’s important to pay attention to that name, because we refer to it in the very last line of the code. You can use the name of your element by changing the name in that line of code.
  3. Paste the code below (essentially, what Brett provided), right inside the $w.onReady function, that is, after the open curly bracket { . That ensures that the page won’t try to display the date until the page elements have all been loaded (the page is “ready”). I added a lot of comments to the code, which you can recognize because they start with // , or are bracketed by /* and */ . Comments are just there to be read by humans, and are otherwise ignored by your website. My comments outline what each bit of the code does. I put all of the code in italics so that it stands out.

/This next line creates a variable called “today”
and sets it to today’s date/

const today = new Date();
// The next three lines format the way the date will appear
const options = {
day: “numeric”,
month: “long”,
year: “numeric”
};
// Sets the property of the text element to be a string representing today’s date in US English
$w(“#text1”).text = today.toLocaleDateString(“en-US”, options);

  1. Just to make sure you understand the placement of the code, your page code, including what wix provides by default, will look like this:

// For full API documentation, including code examples, visit http://wix.to/94BuAAs

$w.onReady( function () {
//TODO: write your page related code here…

/This next line creates a constant called “today”
and sets it to today’s date/

const today = new Date();
// The next three lines format the way the date will appear
const options = {
day: “numeric”,
month: “long”,
year: “numeric”
};
// Sets the property of the text element to be a string representing today’s date in US English
$w(“#text1”).text = today.toLocaleDateString(“en-US”, options);

});

Thank you for this! It works for me.

works fro me! Thank you.