Week | Quarter | Year Calculation using Apex | Aura

 Calculation of current week number, quarter number, and year. Below is an apex code to calculate those details.


Week Number :

Integer daysSince1900_01_07 = Date.newInstance(1900, 1, 7).daysBetween(system.today());

Integer dayNumber = Math.mod(daysSince1900_01_07, 7) + 1;  

Date dateForYear = system.today().addDays(Math.mod(8 - dayNumber, 7) - 3);

Integer yearNum = dateForYear.year();

Date year_01_01 = Date.newInstance(yearNum, 1, 1);       

Integer weekNum = (Integer)Math.floor((year_01_01.daysBetween(system.today())

                                            + Math.mod((Math.mod(Date.newInstance(1900, 1,                                   7).daysBetween(year_01_01), 7) + 1) + 1, 7) - 3) / 7 + 1);

System.debug('Current Week Number is :::'+weekNum);


Quarter Number : 


        Integer quarterNum = [Select Number From Period  Where type = 'Quarter' and StartDate = THIS_FISCAL_QUARTER].Number;

         System.debug('Quarter number:::'+quarterNum);


Current Year : 


    Integer yearNum = Integer.valueOf(system.now().format('Y'));

    System.debug('Current Year:::'+ yearNum);


Bonus : 


Calculation of Current Month, Semi Year, Org Fiscal Month, Org Fiscal Year 


    Integer year_num =integer.valueOf(system.now().format('Y'));

    Integer semiannual;

    Integer orgFiscalMonth = [SELECT FiscalYearStartMonth FROM Organization].FiscalYearStartMonth;

    System.debug('OrgFiscalMonth ::: ' +orgFiscalMonth);

    Date orgFiscalYear = Date.newinstance(system.today().year(), orgFiscalMonth, 1);

    System.debug('OrgFiscalYear ::: '+orgFiscalYear);

    Date orgFiscalYearEnd = orgFiscalYear.addMonths(12).addDays(-1);

    System.debug('OrgFiscalYearEnd ::: ' +orgFiscalYearEnd);

    Date semiyearly = orgFiscalYearEnd.addMonths(-6);

    System.debug('Semiyearlydate :::' +semiyearly);

    Integer monthNum = integer.valueOf(System.now().format('M')) ;      

    System.debug('Current Month :::'+ monthNum);

    Integer yearendmonth = orgFiscalYearEnd.month();

    Integer halfyearmonth = yearendmonth /2;

    if(halfyearmonth >=1 && halfyearmonth<=6){

    semiannual = 1;

    }

    if(halfyearmonth >6&& halfyearmonth<=12){

    semiannual =2;

    }

    System.debug('Year end month :::' +yearendmonth);

    System.debug('Half year month :::' +halfyearmonth);

    System.debug('Semiannual :::' +semiannual);


Calculation of Week Number, Quarter Number, Semi Yearly Number, and Current Year in Aura Component


Use the below code in the Controller class/helper class

 var today = $A.localizationService.formatDate(new Date(), "YYYY-MM-DD");        

component.set('v.today', today);

var date = new Date(today);

//Week number calculation

var tdt = new Date(date.valueOf());

var dayn = (date.getDay() + 6) % 7; 

tdt.setDate(tdt.getDate() - dayn + 3);

var firstThursday = tdt.valueOf();

tdt.setMonth(0, 1);

if (tdt.getDay() !== 4) 

{

 tdt.setMonth(0, 1 + ((4 - tdt.getDay()) + 7) % 7);

}

var currentWeek= 1 + Math.ceil((firstThursday - tdt) / 604800000);        

//Quarter number calculation

var oneJan = new Date(date.getFullYear(), 0, 1);

var month = date.getMonth() + 1;

//Current Year calculation

 var currentYear = date.getFullYear();

 var currentQuarter= Math.ceil(month / 3);

//Semi Yearly Calculation

 if(month>=1 && month<=6){

var currentSemiYearly = 1;

}

 if(month>6 && month<=12){

var currentSemiYearly = 2;

 }     

 component.set("v.weekNum", currentWeek);

 component.set("v.quarterNum", currentQuarter); 

 component.set("v.semiyearNum", currentSemiYearly);

 component.set("v.yearNum", currentYear);




 

Comments

Popular posts from this blog

Customize File Downloads - Salesforce

Access Case by Lightning Platform User License Types (Using Flow) - Salesforce