7

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

4 Answers 4

22

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);
}
3
  • 2
    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.
    – Venky
    Commented May 23, 2017 at 10:10
  • Please could you update your answer to use $A.localizationService.formatDate(new Date(), "YYYY-MM-DD") instead of building the date manually
    – Wes Nolte
    Commented Sep 11, 2018 at 12:21
  • 1
    @WesNolte thank you for the input - updated! (and much cleaner) Commented Sep 11, 2018 at 13:28
2

I have 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);
0

We can use below code as well.

//get today's date
var today = new Date();
var dd = String(today.getDate()).padStart(2, '0');
var mm = String(today.getMonth() + 1).padStart(2, '0'); //January is 0!
var yyyy = today.getFullYear();
today = yyyy + '-' + dd + '-' + mm;`
-3

As above //get today's date var today = new Date(); var dd = String(today.getDate()).padStart(2, '0'); var mm = String(today.getMonth() + 1).padStart(2, '0'); //January is 0! var yyyy = today.getFullYear();

however, if your local time format is different (e.g. UK), change the order.

today = yyyy + '-' + mm + '-' + dd;

You can also add a time if your attribute is of type datetime:

today = yyyy + '-' + mm + '-' + dd + ', 11:45 pm GMT';

or

today = yyyy + '-' + mm + '-' + dd + ', 21:45';

Not that Apex and Javastring formats are different.

apex format “yyyy-MM-dd HH:mm:ss”

javascript format "2019-11-06T10:38:00.000Z"

so, if you are populating a datetime attribute, use

today = yyyy + '-' + mm + '-' + dd + 'T09:30:00';
component.set('v.newEvent.StartDateTime', today);
3
  • 2
    I didn't vote this down, but without clarifying how you got yyyy, mm, and dd it is rather incomplete.
    – Adrian Larson
    Commented Nov 4, 2019 at 14:36
  • Please tell me why I have -2. I'm new to this Exchange and thought I was being helpful. More unexplained negatives and I shall leave the exchange and not bother. Commented Nov 6, 2019 at 16:05
  • 1
    Two people decided your answer is not useful. I am not sure why. Neither was me.
    – Adrian Larson
    Commented Nov 6, 2019 at 16:06

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .