Working Times
Working Times is a node js module to calculate working times and generating working hours calendar.
Working Times helps you to check if any day is working day or weekend or vacation or even it's an exceptional working day.
key features !
- Written in Typescript
- Zero dependencies
- Works on browsers and node js
- Sync and Async
- Supports multiple shifts
- Supports Weekend
- Supports Vacations
- Supports exceptional working days
- Old dates calculations
- Adding working days, hours and minutes to date
- Get date info
- Check if certain time is working time or not ( past & current & future )
- Get next working time from certain time
Release 1.1.* +
Release 1.1.1 +
Release 1.1.2 +
Release 1.1.4 +
Release 1.1.5 +
Release 1.1.10 +
Installation
Install via NPM:
$ npm install working-times
How it works ?
When initializing timer and setting the configuration, timer module will buffer working calendar (from min buffered days to max ) which will contain all information about each day ( is vacation , is exceptional , is weekend , working hours).
On asking timer module about any day it will get this day info if exists in the buffered calendar, if not exists ? timer will extend the buffered calendar ( past or future ) then get day info.
bufferedCalendar : Map <string,BusinessDay> ;
Example
- All Synchronous should be in try and catch block because it will throw an error in case of errors
;// Or ; ; timer.setConfigAsyncVALID_TIMER_CONFIG.then.catch; // Or // All Synchronous should be in try and catch block because it will throw an error in case of errorstry catch error // Or ;iftimer // All Synchronous should be in try and catch block because it will throw an error in case of errorstry catch error // Or timer.getDayInfoAsyncnew Date"07-14-2019".then.catch; // Or ;if dayInfo
DOCS
Construct Timer
To instantiate Timer you have to call getTimerInstance function and all subsequence times you will call this function it will return same instance.
;
Set timer configuration
], 1: , 2: , 3: , 4: }, exceptionalWorkingHours: , minBufferedDays: 1, maxBufferedDays: 1 }
-
Vacations :
vacations should be array list formated date "yyyy-mm-dd".
vacations accepts wildcard year "*-mm-dd". -
NormalWorkingHours :
normal working hours should contain all working week days only ( don't include weekend days ).
object key is day of the week (from 0 to 6), Sunday is 0, Monday is 1, and so on.
object value is array list of working time ( time formate should be "hh:mm" ).normalWorkingHours: INormalWindow ; -
ExceptionalWorkingHours:
exceptional working hours should be list of exceptional working days.
object key is exceptional formated date "yyyy-mm-dd".
exceptional working days accepts wildcard year "*-mm-dd".
object value is array list of working time ( time formate should be "hh:mm" ).exceptionalWorkingHours: IExceptionalWindow ; -
MinBufferedDays:
how many days in the past should be buffered on setting timer configuration. -
MaxBufferedDays:
how many days in the future should be buffered on setting timer configuration.
View buffered calendar
Buffered canedar is Javascript Map of days infos
timer.setConfigAsyncVALID_TIMER_CONFIG.then bufferedCalendar : Map <string,BusinessDay>;
Get date info
To get date info you have to call getDayInfo function and it will return BusinessDay class.
try catch error // Or timerInstance.getDayInfoAsyncday.then.catch; // Or ;
Is working time
To check if a specific time is working time or not you have to call isWorkingTime function and it will return boolean.
try catch error // Or timerInstance.isWorkingTimeAsyncday.then.catch; // Or ;
Get Next Working Time
To next working time from a specific time you have to call getNextWorkingTime function and it will return a Date object
try catch error // Or timerInstance.getNextWorkingTimeAsyncday.then.catch; // Or ;
Add Working Time To Date
To add working time to a specific time you have to call add function and it will return a Date object.
PS: On adding X working days to a date time:
01- If this date time before working hours timer will count this day
02- If this date time is working time, timer will count this day
03- If this date time after working hours timer will start count from tomorrow
timer.add date: Date , duration: number , unit: 'MINUTES'|'HOURS'|'DAYS' ; try catch error // Or timer.addAsyncnew Date"2016-06-16 13:00", 3 , "DAYS".then.catch; // Or ;
Set working timeout
Sets a timer after adding ( X ) working time, which executes the given callback function once the timer expires. Function will return WorkingTimeout Class.
PS: If time in the past after adding ( X ) working duration, the timer will be executed at the soonest working time
timer.setWorkingTimeout baseDate: Date , // new Date() duration: number , // 10 unit: 'MINUTES'|'HOURS'|'DAYS', // MINUTES cb : Function, // ()=>{ console.log('fired')} desc: string // 'Auto reject request #4354' ; // returns WorkingTimeout try catch error // Or You can submit it Async ; timerInstance.setWorkingTimeoutAsyncday , 1 , 'DAYS',cb , 'Timeout for confirm order'.then ;
Get next working day
To get next working day from a specific datetime you have to call getNextWorkingDay function and it will return a Date object ( next day first shift start time )
try catch error // Or timer.getNextWorkingDayAsyncnew Date"2016-06-16 13:00".then.catch; // Or ;
Get previous working day
To get previous working day from a specific datetime you have to call getPreviousWorkingDay function and it will return a Date object ( previous day first shift start time )
try catch error // Or timer.getPreviousWorkingDayAsyncnew Date"2016-06-16 13:00".then.catch; // Or ;
Get Working time between two times
You can calculate working time (Dates , Minutes , Hours ) between two times.
PS: On calculating working days between two dates the function will not calculate the start date if it's a working day so it will be ignored
Get working minutes and hours will return float ( 1.5 = one day and half ...etc )
workingTimeBetween from: Date, to: Date , unit: 'MINUTES'|'HOURS'|'DAYS': Number try catch error // Or timer.workingTimeBetweenAsyncformDay, toDay , "DAYS".then.catch; // Or ;
Get previous working time
Get previous working time will return the latest working minute.
If the requested date is a woriking time, it will return now()
If it's not a working time it will return the latest minute in the pervious window
PS: This method prepared for subtracting working time calculations,
getPreviousWorkingTime date: Date : Date try catch error // Or timer.getPreviousWorkingTimeAsyncday.then.catch; // Or ;
To do
- Subtract working time from given time
- Support Logging
License
MIT