0

How do I modify the code below to have the week range from Thursday 12 PM Central Time Zone to the following Thursday 12 PM, so setting the Start date of the week to be Thursday and end week to be Thursday instead of the Sunday to Sunday?

    Date startWeek = Date.Today().toStartofWeek();
    System.debug(startWeek);
    Date endWeek = Date.Today().toStartofWeek()+6;     
    System.debug(endWeek);
    DateTime dt = DateTime.newInstance(System.today(),Time.newInstance(18,0,0,0));
    String dayOfWeek = dt.format('u');


    //How many days from current week's Thursday is the date?
    Integer offset = Integer.valueOf(dayOfWeek) - 4;
    //If it's before the current weeks' Thursday
    if(offset < 0){
    //Last Thursday should be start date - if you're 3 days away from the current Thursday, you're 4 days away from last thursday (7 days in a week)
    startWeek = Date.Today() - (offset + 7); 
    endWeek = Date.Today() + math.abs(offset);
    } else if (offset > 0) {
    //If it's past current Thursday, apply the opposite logic of the other condition
    startWeek = Date.Today() - offset;
    endWeek = Date.Today() + (7 - offset);
    } else {
    //last condition is that today is Thursday
    startWeek = Date.Today();
    endWeek = Date.Today() + 7;
    }
0

You'd have to do some custom logic as the Date class documentation shows you what toStartofWeek will return which is based on user's locale.

enter image description here

Since it's always Thursday you want to use, you'd have to figure out what day of the week the current day is.

DateTime dt = DateTime.newInstance(System.today(),Time.newInstance(0,0,0,0));
String dayOfWeek = dt.format('u');

This will return:

1 = Monday

2 = Tuesday

3 = Wednesday

4 = Thursday

5 = Friday

6 = Saturday

7 = Sunday

From this, you now know much to add or subtract for start and end. You can utilize time with the understanding that it's in GMT (if you desire 12pm, utilize the GMT of that which is 6pm). Below I use "13" as well since I was executing the code as myself and testing in EST. You may need to consider this depending on where this code executes. I left the System.debug() statements as it shows how it can be helpful to get orientated to how the time is stored.

//setting variables for logic
DateTime startWeek;
DateTime endWeek;
//set this to your desired time
DateTime dt = DateTime.newInstance(System.today(),Time.newInstance(13,0,0,0));
DateTime curr = DateTime.now();
String dayOfWeek = dt.format('u');
Time cutOffTime = Time.newInstance(18,0,0,0);

//How many days from current week's Thursday is the date?
Integer offset = Integer.valueOf(dayOfWeek) - 4;
offset = 1;

System.debug('offset ' + offset + ' and curr ' + curr + ' and dt ' + dt);
//If it's before the current weeks' Thursday
if(offset < 0){
    //Last Thursday should be start date - if you're 3 days away from the current Thursday, you're 4 days away from last thursday (7 days in a week)
    startWeek = dt - (offset + 7); 
    endWeek = dt + math.abs(offset);
} else if (offset > 0) {
    //If it's past current Thursday, apply the opposite logic of the other condition
    startWeek = dt - offset;
    endWeek = dt + (7 - offset);
} else if (offset == 0 && curr.hour() < 18)  {
    //Before the cutoff of the current Thursday
    startWeek = dt.addDays(-7);
    endWeek = dt;
} else {
    //last condition is that today is Thursday and it's past the alloted time
    startWeek = dt;
    endWeek = dt.addDays(-7);
}

System.debug('start date ' + startWeek + ' and end date ' + endWeek);

Important note: above is assuming you're using Thursday. A better approach would be to store the day of the week number (in this case, 4) in a custom setting or custom metadata type. This allows you to change it quickly without any code changes needed in case you change the day of the week you'd like to start or end on.

| improve this answer | |
  • This is very helpful thank you! I have one more question and it may over-complicate the code, but what if I wanted to set the start time as well. I know I would have to include DateTime logic as well, but let's say I want to declare the start of the week on Thursday 11:00 AM CDT until the following Thursday at 11 AM CDT. – Julian Feb 3 at 21:22
  • When you create the DateTime in DateTime dt = DateTime.newInstance(System.today(),Time.newInstance(0,0,0,0));, you can set the time. I put (0,0,0,0) in my example but you can put whatever time you want (hour,minutes,seconds,milliseconds). – Kris Goncalves Feb 4 at 15:00
  • so I tried the code today (Thursday before 12 PM) and it did not work. Please see my edited code in the original question above. I researched the Time.newInstace() and saw that it is UTC timezone so I tired with (12,0,0,0) and (18,0,0,0) since 12 pm Central Time is 18:00 UTC. Any advice? – Julian Feb 6 at 17:15
  • I'll have to look at it again as I didn't really make the code with the time component in my mind. The logic right now is day of the week based so it doesn't take into account 10 on a Thursday vs. 12 on a Thursday. – Kris Goncalves Feb 6 at 17:27
  • I updated it. However, I'm not sure it'll be 100% without taking into account the current user's timezone (whoever is executing the code) depending on how this is displayed or done. If it's run in system mode (e.g. trigger), then you'll want to change the dt time value to 18 instead of 13. – Kris Goncalves Feb 6 at 17:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.