# How to exclude weekends from total number of days in crystal report

By | August 25, 2020

In sage X3, we have a functionality of printing various types of reports such as Order, Invoice, Payments, and Receipt etc. This report is created by using crystal report. Crystal Report is a very powerful tool used to pull data from sage and present it in a ways you want.

Sometimes in crystal report, we may have to develop sales history report. The report should shows the Order No, Invoice No, order date, invoice date, shipment date and how many days it takes to ship an order from the day we received the order until the day it is invoiced. We may also want to calculate how many days early or late based on the date promised to the customer. If the deliveries won’t be done on weekends then in that case we may need to exclude the weekends from the number of days. Refer the below figure of report.

We need to create two different formulas, one for calculating Days to ship and other for Days late.

Calculation Needed:

Days To Ship= Invoice Date – Order Date(exclude weekends)

Days Late= Invoice Date – Promise(Shipment) Date (exclude weekends)

Crystal report has a various types of built in function and operators that includes String function, math function, date and time function, date ranges, arithmetic ,conversion, array operators, etc. We may use that pre-defined function to perform various calculation etc. Some of the Date Time function are as follows:

Date and Time Function includes:

Current Date : This function display the current date.

Current Time: This function display the current time.

DayOfWeek(): This function diplays the day of the specified date.

Syntax: DayOfWeek(date)

DateAdd(): The DateAdd() function can be used to add an interval of time to a date.

Syntax: DateAdd(interval type,number,start date)

DateDiff():The DateDiff() function is used to calculate date difference between two dates.

Syntax: DateDiff(interval type,date 1,date 2)

DatePart(): The DatePart() function returns the part of a date such as a day, month and year.

Syntax: DatePart(date_part,input date)

Interval type description:

yyyy:   years

d:        Day

m:      month

w:      Weekday

ww:   Weeks

q :     quarters

h:       hours

m:     minutes

s:       second

As we have to calculate here difference between two dates we will use DateDiff() function.

Crystal Formula to exclude weekends:

Set Variables for the fields to be used in the calculation.

Define the variable of datatype datetime to be used in the calculation i.e. d1 and d2.

Formula to calculate Days to Ship:

DateTimeVar d1:= {SORDER.ORDDAT};

DateTimeVar d2:={SINVOICE.ACCDAT};

DateDiff(“d”,d1,d2)-DateDiff(“ww”,d1,d2,crSaturday)- DateDiff(“ww”,d1,d2)

Formula to calculate Days Late:

DateTimeVar d1:= {SORDER.SHIDAT};

DateTimeVar d2:={SINVOICE.ACCDAT};

DateDiff(“d”,d1,d2)-DateDiff(“ww”,d1,d2,crSaturday)- DateDiff(“ww”,d1,d2)

Where “d” is no. of days

“ww” is no. of weeks

“crSaturday” is used to exclude all the saturday between  dates

“crSunday” is used to exclude all the sunday between  dates

This blog help us to calculate the number of days between two different dates using date function i.e DateDiff() excluding the weekends.