How can I 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?

  • 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);
    }
    

    Hey @Brian `new Date()` returns system date but not from logged in users timezone. `system.today()` in apex returns date from users timezone, is there any function like that in lightning component.

    Please could you update your answer to use `$A.localizationService.formatDate(new Date(), "YYYY-MM-DD")` instead of building the date manually

    @WesNolte thank you for the input - updated! (and much cleaner)

License under CC-BY-SA with attribution


Content dated before 7/24/2021 11:53 AM