Ask a Question
Ask Question Login
Corporate Training
  1. Community
  2. Salesforce
  3. Question
Salesforce

How can I quickly get today's date in a Lightning Component? (for use in an attribute)

Asked by Ankur Vaish Apr 23, 2021 2.5K views 2 answers
Share

About this question

In Visualforce, we could merge {!TODAY()} directly in the markup. What is the equivalent in a Lightning Component? How can javascript get today's date in lightning component?

Your answer

2 Answers

Ranjana Admin JanBask Expert Latest answer

Answered on Jul 1, 2024

To quickly get today's date in a Lightning Component and use it in an attribute, you can leverage JavaScript within your component's controller or helper. Here’s a step-by-step guide on how to achieve this:


Step 1: Define the Attribute in the Component

First, define an attribute in your Lightning Component to hold today's date.

   

   

    

   

   

Step 2: Set the Date in the Component's Controller or Helper

You need to set the value of the todayDate attribute when the component is initialized. You can do this in the controller or helper.


Option 1: Using Controller (JavaScript)

Create a controller file if it doesn’t already exist, and set the date when the component initializes.

  ({    doInit: function(component, event, helper) {        // Get today's date        let today = new Date();                // Format the date as a string (e.g., YYYY-MM-DD)        let formattedDate = today.toISOString().split('T')[0];                // Set the attribute        component.set("v.todayDate", formattedDate);    }})Option 2: Using Helper (JavaScript)Alternatively, you can set the date in the helper and call it from the controller.javascriptCopy code// Controller({    doInit: function(component, event, helper) {        helper.setTodayDate(component);    }})// Helper({    setTodayDate: function(component) {        // Get today's date        let today = new Date();                // Format the date as a string (e.g., YYYY-MM-DD)        let formattedDate = today.toISOString().split('T')[0];                // Set the attribute        component.set("v.todayDate", formattedDate);    }})

Step 3: Initialize the Component

Ensure that the doInit method is called when the component is initialized.


Explanation

Attribute Definition: The aura:attribute tag defines a component attribute named todayDate to store the date.

Initialization Handler: The aura:handler is used to call the doInit function when the component is initialized.

Controller or Helper Logic: The logic in the controller or helper gets today's date using JavaScript's Date object, formats it to a string (e.g., YYYY-MM-DD), and sets the component attribute todayDate.

This setup ensures that when your Lightning Component is initialized, it automatically gets and displays today's date.

Was this helpful?

More Salesforce discussions

Learn & Explore

Free tutorials and interview questions from industry experts — learn the skill, then get ready to prove it.

Latest Salesforce Blogs

Guides, tips and career advice on Salesforce from JanBask experts.