PS: Calculating Dates

2 minute read

Description:

It is common to need to compare dates or do date calculations in Powershell. The following is how I would go about it:

To Resolve:

  1. This is used in my Credit Balance script:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    
    # Generate all paydays for the year
    [DateTime] $StartDate = "2018-01-05"
    [Int]$DaysToSkip = 14
    [DateTime]$EndDate = "2018-12-31"
    $Arr = @()
    while ($StartDate -le $EndDate) 
    {
    $Arr += $StartDate 
    $StartDate = $StartDate.AddDays($DaysToSkip)
    }
    
    # Remove all paydays that have passed and place remaining in new array
    $NewArr = @()
    ForEach ($Ar in $Arr)
    {
       $s = (Get-Date)
       $e = $Ar
       $Difference = New-TimeSpan -Start $s -End $e
       If ( $Difference -ge 1)
       {
       $NewArr += $Ar
       }
    }
    
    # Select the first one
    $ClosestPayday = $NewArr[0].ToString("yyyy-MM-dd")
    
    # Calculate how many days from now until payday
    $Span = New-TimeSpan -Start (Get-Date) -End $NewArr[0]
    
    # Round up one because there is always a whole number and some change
    $DaysTilPayday = ($Span.Days + 1).ToString()
    
  2. What do you do if you want to calculate a certain day of the month though? That doesn’t always line up to every 30 days. Well, I did some research and this is how I would do “Calculate how many days until the 5th of the month:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    
    $CurrentMonth = $((Get-Date).Month)
    $CurrentDay = $((Get-Date).Day)
    [datetime]$Date = Get-Date -Month $CurrentMonth -Day $CurrentDay
    $ThisFifth = Get-Date -Month $CurrentMonth -Day 5
    
    If ($CurrentMonth -eq 12)
    {
       $CurrentMonthPlusOne = 1
    }
    Else
    {
       $CurrentMonthPlusOne = $($Date.Month) + 1
    }
    [Datetime]$NextFifth = Get-Date -Month $CurrentMonthPlusOne -Day 5
    
    $CurrentYearPlusOne = $($Date.Year) + 1
    [Datetime]$NextFifthIfDecember = Get-Date -Month 1 -Day 5 -Year $CurrentYearPlusOne
    
    $Span = New-TimeSpan -Start $Date -End $ThisFifth
    If ($Span.Days.tostring().startswith('-'))
    {
       If ($CurrentMonth -eq 12)
       {
          $Span = New-TimeSpan -Start $Date -End $NextFifthIfDecember
       }
       Else
       {
          $Span = New-TimeSpan -Start $Date -End $NextFifth
       }
    
    }
    Else
    {
       If ($CurrentMonth -eq 12)
       {
          $Span = New-TimeSpan -Start $Date -End $NextFifthIfDecember
       }
       Else
       {
          # Do nothing as the date is already correct
       }
    }
    Write-Output "The fifth is $($Span.Days) days away!"
    

Comments