function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Siddharth Birari.ax1164Siddharth Birari.ax1164 

Identification of Datetime

Hi,

 

I have a datetime instance available with me.

 

I want to identify '3rd Thursday' for that Month which is represented by that datetime instance.

 

Just like '3rd Thursday', the combination can be any thing provided by the user,

say, "Last Saturday' Or '4th Monday' etc.

 

Please help

Thanks in advance

Best Answer chosen by Admin (Salesforce Developers) 
Siddharth Birari.ax1164Siddharth Birari.ax1164

@Jia Hu: First of all thanks for the reply.

 

But, here your code gives me only 3rd Thu. As I mentioned, the query is dynamic and can be random combination say, 1st Sunday, 2nd Wednesday etc.

 

Although i was going through developing logic only and i came up with this.

 

This method takes four parameters. The first two parameters pMonth, and pYear determine the Month from which you need to pick the date. last two paramters take week Number and day number respectively.

 

    public datetime dtResultant;
    
    public datetime datetimeIdentifier(Integer pMonth, Integer pYear, Integer pWeek, Integer pDay){
        
        if((pMonth >= 0 && pMonth <= 12 && pMonth != null) &&
           (pYear != 0 && pYear != null) &&
           (pWeek >= 0 && pWeek <= 5 && pWeek != null) &&
           (pDay >= 0 && pDay <= 7 && pDay != null)){
        
            dtResultant = datetime.newinstance(pYear, pMonth, 1);
            dtResultant = dtResultant.date().toStartOfWeek();
            
            if(dtResultant.month() < pMonth)
                dtResultant = dtResultant + 7;            
            
            dtResultant = dtResultant.addDays(7 * (pWeek - 1));        //Calculation based on Week Number
            
            if(dtResultant.month() > pMonth)
                dtResultant = dtResultant - 7;            
            
            dtResultant = dtResultant.addDays((pDay - 1));            //Calculation based on Day Number
            
            if((dtResultant.month() > pMonth) ||
               (dtResultant.day() > 7 && pWeek == 1))
                dtResultant = dtResultant - 7;
                
            return dtResultant;
        }

}

All Answers

Jia HuJia Hu
Try this,
Datetime dt = System.now();
If((dt.day() >= 15)&&(dt.day()<=21) && (dt.format('E') == 'Thu') ) {
System.debug('Today is 3rd Thursday ! ');
}
Siddharth Birari.ax1164Siddharth Birari.ax1164

@Jia Hu: First of all thanks for the reply.

 

But, here your code gives me only 3rd Thu. As I mentioned, the query is dynamic and can be random combination say, 1st Sunday, 2nd Wednesday etc.

 

Although i was going through developing logic only and i came up with this.

 

This method takes four parameters. The first two parameters pMonth, and pYear determine the Month from which you need to pick the date. last two paramters take week Number and day number respectively.

 

    public datetime dtResultant;
    
    public datetime datetimeIdentifier(Integer pMonth, Integer pYear, Integer pWeek, Integer pDay){
        
        if((pMonth >= 0 && pMonth <= 12 && pMonth != null) &&
           (pYear != 0 && pYear != null) &&
           (pWeek >= 0 && pWeek <= 5 && pWeek != null) &&
           (pDay >= 0 && pDay <= 7 && pDay != null)){
        
            dtResultant = datetime.newinstance(pYear, pMonth, 1);
            dtResultant = dtResultant.date().toStartOfWeek();
            
            if(dtResultant.month() < pMonth)
                dtResultant = dtResultant + 7;            
            
            dtResultant = dtResultant.addDays(7 * (pWeek - 1));        //Calculation based on Week Number
            
            if(dtResultant.month() > pMonth)
                dtResultant = dtResultant - 7;            
            
            dtResultant = dtResultant.addDays((pDay - 1));            //Calculation based on Day Number
            
            if((dtResultant.month() > pMonth) ||
               (dtResultant.day() > 7 && pWeek == 1))
                dtResultant = dtResultant - 7;
                
            return dtResultant;
        }

}

This was selected as the best answer