2

I need to create a drop down with date values Like

March 28-April 27
February 28-March 27
January 28-February 27
December 28-January 27
November 28-December 27
October 28-November 27
September 28-October 27

any ideas to achieve this ,

This is the code that I have. This code works, but I am wondering if there is a more efficient way to accomplish this

for (Integer i=0;i<9;i++)
    {
        if(i==0)
        {
            optValue = String.valueOf(startdate)+'_'+String.valueOf(enddate);
            optText = startMonth+' '+String.valueOf(startdate.day())+'-'+endMonth+' '+String.valueOf(enddate.day());
            options.add(new selectOption(optValue,optText));
        }
        else
        {
            endDateTime = startDateTime.addDays(-1); // getting the end date from past start date by subtracting 1 day
            enddate = startdate.addDays(-1);
            endMonth = endDateTime.format('MMMMM');

            if(dateValue == 29 && startDateTime.format('MMMMM').equals('February') && !startMonthName.equals('February'))
            {
                if(!date.isLeapYear(startDateTime.year()))  // if it is a leap year february month will have 29 days so we are not adding 1 day to it   
                {
                    startdate = startdate.addMonths(-1).addDays(1);
                }
                else
                {
                    startdate = startdate.addMonths(-1);
                }
            }
            else if(dateValue == 30 && startDateTime.format('MMMMM').equals('February') && !startMonthName.equals('February'))
            {
                if(!date.isLeapYear(startDateTime.year()))  // if it is a leap year february month will have 29 days so we are not adding 2 days to it   
                {
                    startdate = startdate.addMonths(-1).addDays(2);
                }
                else
                {
                    startdate = startdate.addMonths(-1).addDays(1);
                }
            }
            else if(dateValue == 31 && startDateTime.format('MMMMM').equals('February') && !startMonthName.equals('February'))
            {
                if(!date.isLeapYear(startDateTime.year()))  // if it is a leap year february month will have 29 days so we are not adding 3 days to it   
                {
                    startdate = startdate.addMonths(-1).addDays(3);
                }
                else
                {
                    startdate = startdate.addMonths(-1).addDays(2);
                }
            }
            else if(dateValue == 31 && startMonthDays == 31)   //suppose the number of days in the user entered month and datevalue = 31 we check every month and enter the end date of that month
            {
                startdate = startdate.addMonths(-1);
                Integer numberOfDays = date.daysInMonth(startdate.year(),startdate.month());
                startdate = date.newInstance(startdate.year(),startdate.month(),numberOfDays);
            }
            else //this comes under normal condition
            {
                startdate = startdate.addMonths(-1); // getting the start date from past start date by subtracting 1 month
            }
            startDateTime = datetime.newInstance(startdate.year(), startdate.month(), startdate.day());
            startMonth = startDateTime.format('MMMMM');

            optValue = String.valueOf(startdate)+'_'+String.valueOf(enddate); // adding the values to a list
            optText = startMonth+' '+String.valueOf(startdate.day())+'-'+endMonth+' '+String.valueOf(enddate.day());
            options.add(new selectOption(optValue,optText));
        }
5
  • What have you tried to create them so far? Have you familiarised yourself with the Date manipulation methods?
    – Doug B
    Sep 24, 2013 at 14:45
  • Can you explain what you mean by 'the code seems too big'. Did this code fail? Was there an error message? Sep 24, 2013 at 14:49
  • the code didn't failed me, but few advised me to reduce the line of codes . So i thought there might be some other way Sep 24, 2013 at 14:50
  • So this code does work? You're just asking if there is a more efficient way to accomplish this? Sep 24, 2013 at 14:51
  • absolutely . !!! Sep 24, 2013 at 14:52

2 Answers 2

3

Well how about this?

List<SelectOption> options = new List<SelectOption>(); 
datetime d = datetime.newinstanceGMT(2013,3,28);   //Start date for the first period
for (integer x = 0; x < 9; x++) {
    String formattedStart = d.addMonths(-x).formatGMT('MMMMM dd');
    String formattedEnd = d.addMonths(-x+1).addDays(-1).formatGMT('MMMMM dd');
    String fullOption = formattedStart + '-' + formattedEnd;
    System.debug(fullOption);
    options.add(new selectOption(fullOption,fullOption));
}

It produces the same output

DEBUG|March 28-April 27
DEBUG|February 28-March 27
DEBUG|January 28-February 27
DEBUG|December 28-January 27
DEBUG|November 28-December 27
DEBUG|October 28-November 27
DEBUG|September 28-October 27
DEBUG|August 28-September 27
DEBUG|July 28-August 27

The datetime methods are pretty good at coping with month ends. So for example if you start off with 31st October, addMonths(-1) will give you 30th September, addMonths(-2) will give you 31st August. So you don't need to do so much coding yourself unless you have unusual requirements for how you want the date periods calculated.

3
  • This is awesome . Sep 25, 2013 at 5:55
  • sorry to bother you again , while using the date 07/30/2013 after february the date values in the select list gets changed, can u fix this Sep 25, 2013 at 9:49
  • 1
    Sure, I've amended the code now.
    – Doug B
    Sep 25, 2013 at 10:00
0

I assume that you want to somehow build these date ranges dynamically, but you can just use Vf and use a list of SelectOption

In VF

<apex: selectList size="1" value="{!myDateRange}">
     <apex:selectOptions value="{!DateRangeOptions}"/>
</apex:selectList>

Controller

public List<SelectOption> getDateRangeOptions() {

        List<SelectOption> options = new List<SelectOption>(); 
        options.add(new SelectOption('March 28-April 27','March 28-April 27')); 
        options.add(new SelectOption('February 28-March 27','February 28-March 27')); 
        options.add(new SelectOption('January 28-February 27','January 28-February 27')); 
        options.add(new SelectOption('December 28-January 27','December 28-January 27'));
        options.add(new SelectOption('November 28-December 27','November 28-December 27')); 
        options.add(new SelectOption('October 28-November 27','October 28-November 27')); 
        options.add(new SelectOption('September 28-October 27','September 28-October 27'));          
        return options;     
}
1
  • sorry i missed to mention that , i will get the date value from the user , and then i have to calculate the date Sep 24, 2013 at 14:45

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.