Date to Day of the week in String

Date sDate = Date.today();
System.debug(dayOfWeekInString(sDate));

public String dayOfWeekInString(Date startDate)
{
Date myDate = date.newinstance(1900, 1, 7);
Integer daysDifference = myDate.daysBetween(startDate);
Integer currentDayOfWeek = math.mod(daysDifference,7);
String currentDayOfWeekStr;
If(currentDayOfWeek == 0)
    currentDayOfWeekStr = 'Sunday';
else If(currentDayOfWeek == 1)
    currentDayOfWeekStr = 'Monday';
else If(currentDayOfWeek == 2)
    currentDayOfWeekStr = 'Tuesday';
else If(currentDayOfWeek == 3)
    currentDayOfWeekStr = 'Wednesday';
else If(currentDayOfWeek == 4)
    currentDayOfWeekStr = 'Thursday';
else If(currentDayOfWeek == 5)
    currentDayOfWeekStr = 'Friday';
else If(currentDayOfWeek == 6)
    currentDayOfWeekStr = 'Saturday';
    return currentDayOfWeekStr;
}

Comments

Popular posts