PradysTechBlog

Yet another blog

Day of week in apex code of Salesforce

I was looking out for way of getting the day of week of a particular date. On searching on the documentation i couldnt find a way of doing it.

Here is one way of doing it.

There is a function called toStartOfWeek() which returns the start of the week for the Date that called the method, depending on the context user’s locale. For example, the start of a week is Sunday in the United States locale, and Monday in European locales. For example:

date myDate = date.today();
date weekStart = myDate.toStartofWeek();

Now we know the week start date which is going to Monday for European locales and Sunday for US locate. Now we can build up the weekdays for the rest of the week by adding 1 or the no of days, for example you add 2 days to weekstart to get the date for wednesday..

Also i am told there is a much simpler solution though i have not tried it myself.

String shortDayOfweek = DateTime.now().format(‘eee’); // ‘wed’

good luck

Advertisements

One response to “Day of week in apex code of Salesforce

  1. AbdulQadir April 20, 2012 at 9:39 am

    CASE( MOD( CustomDate__c – DATE(1900, 1, 7), 7),
    0, 0,
    6, 0,
    1 ) = 0

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

w

Connecting to %s

%d bloggers like this: