ShowAll Questionssorted byDate Posted

# how do i display the current week dates?

Hi,

I am trying to display dates of current week. How can i do it? What i am trying to do is display the timesheet for the week.

So if i view the page today then it should display me the current week dates. Any pointers to approach this problem would be very helpful

Thanks

Best Answer chosen by Admin (Salesforce Developers)

Find below a sample code to get the week of month :

public Integer currentWeekOfMonth( Date todaysDate){
Date currentDate = todaysDate;
Integer weekCount = 0;
Integer startWeekResidue = 0;
Integer endWeekResidue = 0;
/*
Calculating startWeekResidue
*/
Date dt = currentDate.toStartOfMonth().addDays(-1);
Date dtFirstWeekend = dt.toStartOfWeek().addDays(6);
startWeekResidue = dt.daysBetween(dtFirstWeekend);
/*
Calculating endWeekResidue
*/
Date dtLastWeekend = currentDate.toStartOfWeek().addDays(-1);
endWeekResidue = dtLastWeekend.daysBetween(currentDate);
/*
Counting the weeks
*/
weekCount = (currentDate.day() - (startWeekResidue + endWeekResidue))/7;
weekCount += (startWeekResidue > 0 ? 1:0)+(endWeekResidue > 0 ? 1:0);
System.debug(weekCount);
return weekCount;
}

EIE50

Hi,

Can you post your code?

Thanks.

Manu Erwin

I'd start with an Apex method that outputs a List collection of Dates making use of System.Today() and the toStartOfWeek() instance method to figure out the start of the week (depending on locale) then looping to build up the 7 dates for that week.

You could then iterate over that collection in the VF page with an apex:repeat tag, although depending on how you're wanting to display them (i.e., dates are more often repeated horizontally than vertically) you'll need to do some clever stuff with CSS (I've used float in the past to achieve this kind of effect).

Hope that helps.

Manu

Find below a sample code to get the week of month :

public Integer currentWeekOfMonth( Date todaysDate){
Date currentDate = todaysDate;
Integer weekCount = 0;
Integer startWeekResidue = 0;
Integer endWeekResidue = 0;
/*
Calculating startWeekResidue
*/
Date dt = currentDate.toStartOfMonth().addDays(-1);
Date dtFirstWeekend = dt.toStartOfWeek().addDays(6);
startWeekResidue = dt.daysBetween(dtFirstWeekend);
/*
Calculating endWeekResidue
*/
Date dtLastWeekend = currentDate.toStartOfWeek().addDays(-1);
endWeekResidue = dtLastWeekend.daysBetween(currentDate);
/*
Counting the weeks
*/
weekCount = (currentDate.day() - (startWeekResidue + endWeekResidue))/7;
weekCount += (startWeekResidue > 0 ? 1:0)+(endWeekResidue > 0 ? 1:0);
System.debug(weekCount);
return weekCount;
}

This was selected as the best answer

Thanks a lot...

This is more than what i had asked for.. I just needed some pointers on how to go about doing it.. Pradeep just gave me the code...

Thanks for all the help..

Regards