Showing posts with label Calculate Day of Date in Apex. Show all posts
Showing posts with label Calculate Day of Date in Apex. Show all posts

Monday, 6 June 2011

Calculate Day of Date in Salesforce

In Apex Class :

Way 1 :
Calculate using DateTime Format : It is very interesting way of finding the Day of a date.
Date d = date.newinstance(1947,8, 15);
datetime myDate = datetime.newInstance(d.year(), d.month(), d.day());
String day = myDate.format('EEEE');


Way 2 : Prepared a class to find out day of given date using Mod of  7 to determine the day.

public class DayOfDate {
    Private Date startDate = date.newInstance(0001, 1, 1);
    Public Date selectedDate{get;set;}
    public String dayValue{get;set;}
    public Account acc {get;set;}
    public DayOfDate()
    {
        selectedDate = date.today();
        acc = new Account();
    }

    public void calculateDayOfDate()
    {
        List<String> listDay = new List<String>{'Saturday' , 'Sunday' , 'Monday' , 'Tuesday' , 'Wednesday' , 'Thursday' , 'Friday'};
        selectedDate = acc.SLAExpirationDate__c;
        Integer remainder = Math.mod(startDate.daysBetween(selectedDate) , 7);
        dayValue = listDay.get(remainder);
    }



}


Visualforce Page :
<apex:page controller="DayOfDate">
  <apex:form>
  <apex:pageBlock>
      
      <apex:pageBlockSection columns="1">
          <apex:pageBlockSectionItem>
              <apex:outputLabel value="Enter Date"></apex:outputLabel>
              <apex:inputField value="{!acc.SLAExpirationDate__c}"/>
          </apex:pageBlockSectionItem>
          <apex:pageBlockSectionItem>
              <apex:outputLabel value="Day is : {!dayValue}"></apex:outputLabel>
          </apex:pageBlockSectionItem>
          <apex:commandButton action="{!calculateDayOfDate}" value="Find Day"/>
          
      </apex:pageBlockSection>
  </apex:pageBlock>
  </apex:form> 
</apex:page>

In above example I used Account objects date field  SLAExpirationDate__c to take input from user, you can take input using text field also, but in that case please format the date properly.


Calculate Day of a Date in formula field :

Create a formula field which should have return type 'Text'

IF(MOD(DateFieldAPIName__c - DATE(0001,1,1) , 7) == 0 , 'Saturday' , IF(MOD(DateFieldAPIName__c- DATE(0001,1,1) , 7) == 1 , 'Sunday' , 
IF(MOD(DateFieldAPIName__c- DATE(0001,1,1) , 7) == 2 , 'Monday' , 
IF(MOD(DateFieldAPIName__c- DATE(0001,1,1) , 7) == 3 , 'Tuesday' , 
IF(MOD(DateFieldAPIName__c- DATE(0001,1,1) , 7) == 4 , 'Wednesday' , 
IF(MOD(DateFieldAPIName__c- DATE(0001,1,1) , 7) == 5 , 'Thursday' , 
IF(MOD(DateFieldAPIName__c- DATE(0001,1,1) , 7) == 6 , 'Friday' , 'No Day')))))) 
)

In this formula DateFieldAPIName__c is the field api name of the date field for which you want to know the day of date.


You can also Use "Case" instead of  IF in in above formula like this :

CASE(MOD(DateFieldAPIName__c - DATE(0001,1,1) , 7) , 0 , 'Saturday' , 1 , 'Sunday' ,  2 , 'Monday' , 3 , 'Tuesday' , 4 , 'Wednesday' , 5 , 'Thursday' , 6 , 'Friday'  , 'No Day Found')
Regards
Shashikant Sharma

Tweet