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
Deepak Pandey 13Deepak Pandey 13 

I want to retrieve day name from date/time data type?

Hi 
  I am trying to get day name from date type data type.

<aura:component>
  <aura:handler name="init" value="{!this}" action="{!c.doInit}"/>
 <aura:attribute name="today" type="Date" default=""/>
 
    <ui:inputDate aura:id="expdate" label="Today's Date" class="field" value="{!v.today}" displayDatePicker="true" />      
    <ui:button class="btn" label="Submit" press="{!c.setOutput}"/> 

 <div aura:id="msg" class="hide">
  You entered: <ui:outputDate aura:id="oDate" value="" />
 </div>
     
</aura:component>

Controller is-

({
    doInit : function(component, event, helper) {
        var today = new Date();
        component.set('v.today', today.getFullYear() + "-" + (today.getMonth() + 1) + "-" + today.getDate());
    },

    setOutput : function(component, event, helper) {
        var cmpMsg = component.find("msg");
        $A.util.removeClass(cmpMsg, 'hide');
        var expdate = component.find("expdate").get("v.value");

        var oDate = component.find("oDate");
        oDate.set("v.value", expdate);

    }
  
})  

This code retrive all day which you given. I want this code only show Day name like sunday , monday ETC
Best Answer chosen by Deepak Pandey 13
Deepak Pandey 13Deepak Pandey 13
({
   setutput : function(component, event, helper) {
        var cmpMsg = component.find("msg");
        $A.util.removeClass(cmpMsg, 'hide');
     var expdate = component.find("expdate").get("v.value");    
        alert(expdate); 
        var a = new Date(expdate);
        var days = new Array(7);
        days[0] = "Sunday";
        days[1] = "Monday";
        days[2] = "Tuesday";
        days[3] = "Wednesday";
        days[4] = "Thursday";
        days[5] = "Friday";
        days[6] = "Saturday";
        var r = days[a.getDay()];
        alert(r);
        var oDate = component.find("oDate");
        oDate.set("v.value", r);
    }
  
})

All Answers

Dilip_VDilip_V
Deepak,

Try this code:
Datetime dt = DateTime.newInstance(Date.today(), Time.newInstance(0, 0, 0, 0));
String dayOfWeek=dt.format('EEEE');
System.debug('Day : ' + dayOfWeek);

Source:
https://developer.salesforce.com/forums/?id=906F00000008zEmIAI

Let us know if you have any issues.

Mark it as best answer if it works.

THanks.
Deepak Pandey 13Deepak Pandey 13
Hi Thermo Dynamics
You are right but its work in apex class but i want to to call component controller which is invoke a method which is give the name of day component output field.
thanks for reply.
Deepak Pandey 13Deepak Pandey 13
({
   setutput : function(component, event, helper) {
        var cmpMsg = component.find("msg");
        $A.util.removeClass(cmpMsg, 'hide');
     var expdate = component.find("expdate").get("v.value");    
        alert(expdate); 
        var a = new Date(expdate);
        var days = new Array(7);
        days[0] = "Sunday";
        days[1] = "Monday";
        days[2] = "Tuesday";
        days[3] = "Wednesday";
        days[4] = "Thursday";
        days[5] = "Friday";
        days[6] = "Saturday";
        var r = days[a.getDay()];
        alert(r);
        var oDate = component.find("oDate");
        oDate.set("v.value", r);
    }
  
})
This was selected as the best answer