logo

Calculating dates taking into account the business calendar

This article describes working with dates in scripts taking into account the business calendar.

For the scripts to work, add namespaces:

using EleWise.ELMA.Scheduling; 
using EleWise.ELMA.Services;
 
1. Calculate the interval between two dates taking into account the business calendar (this method returns the number of working days and remaining working hours):
//set the period start 
DateTime startTime = DateTime.Now; 
//set the period end 
DateTime endTime = (DateTime)context.EndDate;
//get the service of the business calendar 
var calendar = Locator.GetServiceNotNull<IProductionCalendarService>();
//calculate the time interval with the method EvalWorkTimeDifference() 
context.TimeInterval = calendar.EvalWorkTimeDifference(startTime, endTime);
 

2. Calculate the time interval between dates taking into account the business calendar (this method returns the number of working days without working hours):

//set the period start 
DateTime startTime = DateTime.Now; 
//set the period end 
DateTime endTime = (DateTime)context.EndDate;
//get the business calendar service 
var calendar = Locator.GetServiceNotNull<IProductionCalendarService>();
//calculate the interval with the method EvalWorkTimeSpanDifference() 
context.TimeInterval = calendar.EvalWorkTimeSpanDifference(startTime, endTime);
 

3. Add time taking into account the business calendar (only working hours are taken into account):

//get the business calendar service 
var calendar = Locator.GetServiceNotNull<IProductionCalendarService>();
//add 4 working hours to the context variable PeriodStart 
//write the result to the context variable PeriodEnd
//the variables PeriodStart and PeriodEnd have the Date/Time type 
context.PeriodEnd = calendar.EvalTargetTime(context.PeriodStart.Value, TimeSpan.FromHours(4));
 
Creating a similar script with PublicAPI
Documentation on PublicAPI is available here
Attention
The script below is relevant for ELMA 3.8 to 3.12.1 inclusive.
1. Calculate the interval between two dates taking into account the business calendar (this method returns the number of working days and remaining working hours):
//set the period start
DateTime startTime = DateTime.Now;
//set the period end
DateTime endTime = (DateTime)context.EndDate;
//calculate the interval with the method EvalWorkTimeDifference()
context.TimeInterval = PublicAPI.Services.ProductionCalendar.EvalWorkTimeDifference(startTime, endTime);
 

2. Calculate the time interval between dates taking into account the business calendar (this method returns the number of working days without working hours):

//set the period start
DateTime startTime = DateTime.Now;
//set the period end
DateTime endTime = (DateTime)context.EndDate;
//calculate the interval with the method EvalWorkTimeSpanDifference()
context.TimeInterval = PublicAPI.Services.ProductionCalendar.EvalWorkTimeSpanDifference(startTime, endTime);
 

3. Add time taking into account the business calendar (only working hours are taken into account):

//add 4 working hours to the context variable PeriodStart
//write the result to the context variable PeriodEnd
//the variables PeriodStart and PeriodEnd have the Date/Time type
context.PeriodEnd = PublicAPI.Services.ProductionCalendar.EvalTargetTime(context.PeriodStart.Value, TimeSpan.FromHours(4));