+ Start a Discussion
Mitchell McLaughlin 10Mitchell McLaughlin 10 

Get Current Time in Lightning Component

Hello All,

I'm trying to get EST current time oninit whenever my lightning component is opened. I have concerns about pulling the time in the user's timezone as they are not necessarily in eastern. I also have concerns about pulling the general time of GMT which is used in SFDC development because of daylight savings.

Basically, I want to always dynamically hide a component if it is before 8:30 in the morning. 

Please help! Thanks.
Anant KamatAnant Kamat
Hi Mitchell,
Kindly refer the below 2 links. Hope these are helpful to you

https://salesforce.stackexchange.com/questions/164230/how-can-i-quickly-get-todays-date-in-a-lightning-component-for-use-in-an-attr
https://salesforce.stackexchange.com/questions/175573/how-to-get-current-date-as-per-logged-in-user-in-lightning-component/175677
Deepali KulshresthaDeepali Kulshrestha
Hi Mitchell,

Follow below code, it will help you to show date in lightning component

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

I suggest to visit these links,it will help you

https://salesforce.stackexchange.com/questions/200889/how-to-get-todays-date-in-a-lightning-component-based-on-their-server-side-time

https://salesforce.stackexchange.com/questions/164230/how-can-i-quickly-get-todays-date-in-a-lightning-component-for-use-in-an-attr/164231

https://www.biswajeetsamal.com/blog/salesforce-lightning-formatted-datetime/

I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

Thanks and Regards,
Deepali Kulshrestha.