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? How can javascript get today's date in lightning component?
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