function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
punith narasimhapunith narasimha 

How to Insert Date and Time field in lightning

hi 
i am crated a component page with "Enter a date" is date field, 'Starttime' is time sloat  now combined these 2 fields and set as date and time format in controller and  want to insert a student record 

component.set("v.student.Start_Date__c",------  ? ? ? -----);
please help me 
Thank you

User-added image
Deepali KulshresthaDeepali Kulshrestha
Hi Punith,
You can use the lightning:formattedDateTime as per your requirement.Please refer to the following code as it may be helpful in solving your problem:
Component:
<aura:component >
    <!--Declare Attribute-->
    <aura:attribute name="currentDate" type="Date"/>
     
    <!--Declare Handlers-->
    <aura:handler name="init" value="{!this}" action="{!c.doInit}"/>
     
    <!--Component Start-->
    <div class="slds-m-around_xx-large">
        <lightning:formattedDateTime aura:id="dt"
                                     value="{!v.currentDate}"
                                     month="short"
                                     day="numeric"
                                     year="numeric"
                                     hour="2-digit"
                                     minute="2-digit"
                                     second="2-digit"
                                     hour12="true"
                                     timeZone="{!$Locale.timezone}"/>
    </div>
    <!--Component End-->
</aura:component>

Controller:
({
    doInit : function(component, event, helper) {
        var today = new Date();
        component.set('v.currentDate', today);
    }
})

For more information on lightning:formattedDateTime you can go through the following links:
https://developer.salesforce.com/docs/component-library/bundle/lightning:formattedDateTime/example#lightningcomponentdemo:exampleDateTimeFormatting
https://developer.salesforce.com/docs/component-library/bundle/lightning:formattedDateTime/documentation

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