Todays Date

504/5000

I created an element in my form that should generate a field in a database. This should be generated today’s date.
I followed the guidelines. I copied and pasted the code below.
$ w.onReady (function () {
// Gets today’s date
const today = new Date ();
// Sets the property of the text element to be the string representing today’s date in the user’s local format
$ w (‘# input20’). text = today.toLocaleDateString ();

});
input20 is the field that will receive the date.
But the date is not appearing in the field in question.
Help-me. Please.

Do not put any spaces like you did in your code above

$w.onReady(function () {
 const today = new Date ();
    $w("#input20").text = today.toLocaleDateString();
});

Also, check if your input20 is actually an input element or text

Can you copy, correct and show me? I can not see where the spaces are.
Thank you.

Edison the spaces are showing between $ and w and also . and text. The wix code Editor is probably underlining this section of code and flagging an error.

Do as Shan says and you should be good.

O que tem de errado com esse código?

$w.onReady( function () {
// Gets today’s date
const today = new Date();
// Sets the property of the text element to be a string representing today’s date in the user’s local format
$w(‘#input20’) = today.toLocaleDateString();

});
Ele deveria retornar a data de hoje no campo ‘input20’
Mas não retorna.

@etchagas
This line is incorrect:

$w('#input20') = today.toLocaleDateString();

The input field should be set to a value. A text field should be set to text or html. Since I assume input20 is an input field then you should use value :slight_smile:

$w('#input20').value = today.toLocaleDateString();

What you need to do is get used to reading the API documentation to tell you how to use the element. These are the links for this problem:

  • Input Text

  • Static Text (plain text)

  • Static Text (rich text - html)


When you are using code the best thing to do is hover your mouse over the api function name and you will see a pop-up like this:

<— Hover over any element property or function name and you have quick access to the documentation. You can also do this when the auto complete for the element is displayed.

Its always a good idea to read these documents when you are trouble shooting. You will probably solve many of your challenges this way.

Cheers

This space only appears here in the forum. On the page it does not exist.
I copied and pasted this code from wix’s own help page. I just substituted the field name for “input20”.

@stevendc
This is an image of my page at the moment.
The field that will receive the date (input20), was created as input field in the menu “Add”

after input should be .value not text becasue as Steve wrote input has value not text

OK! It worked.
Thank you to all who have manifested.
Now I’m going to search how to add time / ninuto in the same field.
Thus: date - hh: mm

instead to localeDeteString write tolocaleString() and You have
2.01.2019, 00:00:00

Yeahhh! Thanks!

I’m having the same problem and am using the correct code, but its still not working. any chance you could tell me how to make sure my input20 is actually an input element?

@kate31 I can’t see it properly.

Anyways, try this

$w("#datetoday").text