[SalesForce] How to quickly get today’s date in a Lightning Component? (for use in an attribute)

In Visualforce, we could merge {!TODAY()} directly in the markup. What is the equivalent in a Lightning Component?

Best Answer

You create an attribute and initialize it using $A.localizationService in your controller.

Component:

<aura:component>
   <aura:handler name="init" action="{!c.init}" value="{!this}" />
   <aura:attribute name="today" type="Date" />
   <ui:outputDate value="{!v.today}" />
</aura:component>

Controller:

init : function(component, event, helper) {
    var today = $A.localizationService.formatDate(new Date(), "YYYY-MM-DD");
    component.set('v.today', today);
}