Git Product home page Git Product logo

psbusinesstime's Introduction

PSBusinessTime

Branch Build status Last commit Latest release PowerShell Gallery GitHub
main GitHub Workflow Status (main) GitHub last commit (branch) GitHub release (latest by date) GitHub Release Date PowerShell Gallery GitHub all releases

A PowerShell module with functions to help calculate working hours, and for generally working with or calculating business hours / date times.

Inspired by https://pleasework.robbievance.net/howto-calculate-elapsed-business-hours-using-powershell/.

Functions

Requirements

  • PowerShell 5.1 or newer

Getting started

Install and import:

Install-Module PSBusinessTime -Scope CurrentUser
Import-Module PSBusinessTime

See the below examples and use Get-Help to learn more about the functions with examples. You can discover all functions in the module by running:

Get-Command -Module PSBusinessTime

Examples

New-BusinessTimeSpan -Start (Get-Date '2022-04-01 00:00:00') -End (Get-Date '2022-04-30 23:59:59') -NonWorkingDates (Get-Date '2022-04-15'), (Get-Date '2022-04-18')

The function will return a timespan object of 171 hours:

Days              : 7
Hours             : 3
Minutes           : 0
Seconds           : 0
Milliseconds      : 0
Ticks             : 6156000000000
TotalDays         : 7.125
TotalHours        : 171
TotalMinutes      : 10260
TotalSeconds      : 615600
TotalMilliseconds : 615600000

2022-04-01 through 2022-04-30 is an entire calendar month, and only 171 hours is considered "working hours" within the defined parameters. '2022-04-15' and '2022-04-18' are considered non-working dates.


Test-WorkingDay -Date (Get-Date '2022-04-11 09:00:00')

The function will return true because the datetime is within the default parameters. 2022-04-11 is a Monday, and 09:00 is between 08:00 and 17:00.


Get-WorkingDates -Start (Get-Date '2022-04-04') -End (Get-Date '2022-04-17') -NonWorkingDaysOfWeek 'Saturday','Sunday','Monday'

The function will return an array of 8 datetime objects for '2022-04-05' through to '2022-04-08', and '2022-04-12' through to '2022-04-15':

05 April 2022 00:00:00
06 April 2022 00:00:00
07 April 2022 00:00:00
08 April 2022 00:00:00
12 April 2022 00:00:00
13 April 2022 00:00:00
14 April 2022 00:00:00
15 April 2022 00:00:00

These are considered working dates within the defined parameters. Saturdays, Sundays, and Mondays, are considered non-working days, therefore every other date inbetween the range is considered a working date.

To-do

  • Use some public API for public holidays
  • Allow configurable 'lunch hour' and so it respects the designated time period, too, e.g.
    • if start date is 8am and end date is 5pm, and 'lunch hour' time period is 12pm - 1pm, then working hours is 8.
    • if start date is 12pm and end date is 5pm, and 'lunch hour' time period is 12pm - 1pm, then working hours is 4.
  • Allow config for specifying days with different working hours, e.g. perhaps specific dates of the month or year might be 4 or 5 working hours rather than 8 or 9, which is generally days like Christmas Eve is most western countries

psbusinesstime's People

Contributors

actions-user avatar codaamok avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Forkers

taxitorloseit

psbusinesstime's Issues

Method invocation failed because [System.TimeSpan] does not contain a method named 'op_Multiply'.

Environment

Name                      Value                  
----                      -----                  
PSVersion                 5.1.19041.1682         
PSEdition                 Desktop                
PSCompatibleVersions      {1.0, 2.0, 3.0, 4.0...}
BuildVersion              10.0.19041.1682        
CLRVersion                4.0.30319.42000        
WSManStackVersion         3.0                    
PSRemotingProtocolVersion 2.3                    
SerializationVersion      1.1.0.1                

Current (as of 0.3.0)

Input

New-BusinessTimeSpan -Start (Get-Date '2022-04-01 00:00:00') -End (Get-Date '2022-04-30 23:59:59') -NonWorkingDates (Get-Date '2022-04-15'), (Get-Date '2022-04-18')

Output

Method invocation failed because [System.TimeSpan] does not contain a method named 'op_Multiply'.
At C:\Users\...\Documents\WindowsPowerShell\Modules\PSBusinessTime\0.3.0\PSBusinessTime.psm1:409 char:9
+         $InBetweenHours = $NumberOfWorkingDays * $WorkingHours
+         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (op_Multiply:String) [], RuntimeException
    + FullyQualifiedErrorId : MethodNotFound

Workaround

Patch

PSBusinessTime.psm1
Line 313

From:

$WorkingHours = New-TimeSpan -Start $StartHour -End $FinishHour

To:

$WorkingHours = (New-TimeSpan -Start $StartHour -End $FinishHour).TotalHours

PSBusinessTime.psm1
Line 409

From:

$InBetweenHours = $NumberOfWorkingDays * $WorkingHours

To:

$InBetweenHours = New-TimeSpan -Hours ($NumberOfWorkingDays * $WorkingHours)

Input

New-BusinessTimeSpan -Start (Get-Date '2022-04-01 00:00:00') -End (Get-Date '2022-04-30 23:59:59') -NonWorkingDates (Get-Date '2022-04-15'), (Get-Date '2022-04-18')

Output

Days              : 7
Hours             : 3
Minutes           : 0
Seconds           : 0
Milliseconds      : 0
Ticks             : 6156000000000
TotalDays         : 7.125
TotalHours        : 171
TotalMinutes      : 10260
TotalSeconds      : 615600
TotalMilliseconds : 615600000

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.