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
Mangi S V V Satya Surya Sravan KumMangi S V V Satya Surya Sravan Kum 

Can anyone help me to find saturday and sunday of a given month

Hello,  I have been trying this program since last 4 hours I'm stuck with it please some one help me 
Best Answer chosen by Mangi S V V Satya Surya Sravan Kum
Suraj Tripathi 47Suraj Tripathi 47
Hi Mangi,

Check this code:-
public class NumberTOWordConvertion {
    public static void method(){
        Date dt = Date.newInstance(2020, 6, 1);//user give any month
        
        Integer month = dt.Month(); 
        Integer year = dt.Year(); 
        Integer numberDays = Date.daysInMonth(year, month);
        Integer totalSun = 0;
        Integer totalSat = 0;
        for(Integer i=1;i<=numberDays ;i++)
        {
            Date myDate = date.newInstance(year, month, i);
            Datetime dt1 = (DateTime)myDate;
            if(String.valueOf(dt1.format('EEEE')) == 'Sunday')
            {
                totalSun = totalSun+1;  
            }
            if(String.valueOf(dt1.format('EEEE')) == 'Saturday')
            {
                totalSat = totalSat+1;  
            }
        }
        
        System.debug('totalSun -->> '+totalSun);
        System.debug('totalSat -->> '+totalSat);
    }
}

In case you find any other issue please mention. 
If you find your Solution then mark this as the best answer. 

All Answers

AbhinavAbhinav (Salesforce Developers) 
Hi,

Have you check this link?

https://developer.salesforce.com/forums/?id=9062I000000gBceQAE

Thanks!
CharuDuttCharuDutt
Hii Mangi
Try Below Code
Integer month = 10;  
Integer year = 2019;
Integer numberDays = Date.daysInMonth(year, month);
Integer noOfSun = 0;

for(Integer i=1;i<=numberDays ;i++)
{
    Date myDate = date.newInstance(year, month, i);
    Datetime dt = (DateTime)myDate;
    if(dt.format('EEEE' == 'Sunday')
    {
        noOfSun = noOfSun + 1;  
    }
}
Please Mark It As Best Answer If It Helps
Thank You!

 
mukesh guptamukesh gupta
Hi Mangi,

Please use below code 100% tested 
 
Date dt = System.today();

Integer month = dt.Month(); 
Integer year = dt.Year(); 
Integer numberDays = Date.daysInMonth(year, month);
Integer totalSun = 0;
Integer totalSat = 0;

for(Integer i=1;i<=numberDays ;i++)
{
    Date myDate = date.newInstance(year, month, i);
    Datetime dt = (DateTime)myDate;
    
   if(dt.format('EEEE') == 'Sunday')
    {
        totalSun = totalSun + 1;  
    }
    if(dt.format('EEEE') == 'Saturday')
    {
        totalSat = totalSat + 1;  
    }
}

System.debug('totalSun -->> '+totalSun);
System.debug('totalSat -->> '+totalSat);


if you need any assistanse, Please let me know!!


Kindly mark my solution as the best answer if it helps you.

Thanks
Mukesh
Mangi S V V Satya Surya Sravan KumMangi S V V Satya Surya Sravan Kum
Hi Mukesh, Your Code is working but User need to give the month, not as system default month.
                    Can you help me.
                     THANKS
 
Suraj Tripathi 47Suraj Tripathi 47
Hi Mangi,

Check this code:-
Date dt = Date.newInstance(2020, 6, 1);//user give any month

Integer month = dt.Month(); 
Integer year = dt.Year(); 
Integer numberDays = Date.daysInMonth(year, month);
Integer totalSun = 0;
Integer totalSat = 0;

for(Integer i=1;i<=numberDays ;i++)
{
    Date myDate = date.newInstance(year, month, i);
    Datetime dt = (DateTime)myDate;
    
   if(dt.format('EEEE') == 'Sunday')
    {
        totalSun = totalSun++;  
    }
    if(dt.format('EEEE') == 'Saturday')
    {
        totalSat = totalSat++;  
    }
}

System.debug('totalSun -->> '+totalSun);
System.debug('totalSat -->> '+totalSat);
In case you find any other issue please mention. 
If you find your Solution then mark this as the best answer. 

 
Mangi S V V Satya Surya Sravan KumMangi S V V Satya Surya Sravan Kum
Hi Suraj,

The was zero in output for both saturday and sunday , no number was showen.
Please help me
 
Suraj Tripathi 47Suraj Tripathi 47
Hi Mangi,

Check this code:-
public class NumberTOWordConvertion {
    public static void method(){
        Date dt = Date.newInstance(2020, 6, 1);//user give any month
        
        Integer month = dt.Month(); 
        Integer year = dt.Year(); 
        Integer numberDays = Date.daysInMonth(year, month);
        Integer totalSun = 0;
        Integer totalSat = 0;
        for(Integer i=1;i<=numberDays ;i++)
        {
            Date myDate = date.newInstance(year, month, i);
            Datetime dt1 = (DateTime)myDate;
            if(String.valueOf(dt1.format('EEEE')) == 'Sunday')
            {
                totalSun = totalSun+1;  
            }
            if(String.valueOf(dt1.format('EEEE')) == 'Saturday')
            {
                totalSat = totalSat+1;  
            }
        }
        
        System.debug('totalSun -->> '+totalSun);
        System.debug('totalSat -->> '+totalSat);
    }
}

In case you find any other issue please mention. 
If you find your Solution then mark this as the best answer. 
This was selected as the best answer
Alex Jones 32Alex Jones 32
looks like solutions above work... I try to use the code on some pages of my project (https://onlinecasinos-australia.com/casino-payment/bpay.html). Also noticed that there was zero in output for both  saturday and sunday...
Mir soyelMir soyel
good content
dainikchorcha (https://www.dainikchorcha.com)
codecary (https://www.codecary.com)