2

I'm using lightning:input type="date" for a date field, but I'm not able to set today date at first. My code is this:

    var oggi = new Date();
    var today = oggi.getFullYear() + "/" + (oggi.getMonth() + 1) + "/" + oggi.getDate();
    component.find("mydateId").set("v.value", today);

and cmp code is:

<lightning:input aura:id="mydateId" type="date" label="Data Inizio"/>
  • 2
    shouldn't you be setting the value to v.dataInizio instead of v.value? – glls Sep 19 '17 at 14:09
  • it doesn't work even so – Noomak Sep 19 '17 at 14:17
  • do you have an attribute for it? oh nooes, you edited and removed v.dataInizio, :flip table: have you tried following the documentation, you are missing bits of code, where is your attribute? – glls Sep 19 '17 at 14:17
  • check Lightning Components Developer Guide for ui:inputDate the examples there should help you – glls Sep 19 '17 at 14:20
  • 1
    did you end up adding an attributre? and check the examples from the doc i linked? – glls Sep 19 '17 at 14:27
2

A good start for learning how to lightning components work would be through Trailheads Lightning Components Basics

You should have (or should) declare an attribute for your date as specified in the doc I linked for ui:inputDate -> <aura:attribute name="attrName" type="Date" default="" /> and in your controller set the value accordingly:

component.set('v.attrName', yourValue) 

you will need to reference it in your lightning:input component as you had done originaly using v.dataInizio however, you seem to have changed the attribute name to value for some reason.

here is a little screenshot of both components compared : enter image description here

notice that the date formatting has to be formatted as YEAR-MM-DD for lightning:input to accept the value or it will default to:

enter image description here

and yes, if you use an aura:id, you can fetch your component and set a value to it without using attributes.

  • 1
    The SET is done through component.find("mydateId").set("v.value", today). The attribute is not mandatory in this case, you can easily use aura:id – Noomak Sep 19 '17 at 15:42
  • overall, in your post, the code is incomplete, we don't even know how you are calling your controller function, no attributes, maybe its your date format. I copy pasted your code and followed best practices, and am able to populate a ui:inputDate as per lightning:input of type date, your date formatting seems off. – glls Sep 19 '17 at 16:23
  • @Noomak, were you able to make it work? did you validate that your date formatting was correct for the lightning:input component? – glls Sep 19 '17 at 18:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.