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

1.1K    Asked by ankur_3579 in Salesforce , Asked on Apr 23, 2021

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?

Answered by anu rhea

You need a slight modification to the JS you proposed, as you will run into comparison errors if the day is a single digit as well. Modifying the script like so fixed the problem:

      var today = new Date(); var monthDigit = today.getMonth() + 1; if (monthDigit <= 9) { monthDigit = '0' + monthDigit; } var dayDigit = today.getDate(); if(dayDigit <= 9){ dayDigit = '0' + dayDigit; } component.set('v.today', today.getFullYear() + "-" + monthDigit + "-" + dayDigit);

This way javascript get today's date



Your Answer

Interviews

Parent Categories