PoshCode Archive  Artifact [8e55d20dbb]

Artifact 8e55d20dbbea238ccf3d63a20202d6bbab272040921773e90575b13e35d1df69:

  • File Get-Time-Between-Dates.ps1 — part of check-in [33b188d7e1] at 2018-06-10 14:22:09 on branch trunk — Provide a begin and end time frame (mm dd yyyy) and this will calculate the years, months and days between the two dates. This is NOT 100% accurate but it was close enough for what I was trying to do. Someone with better math skills should be able to improve the accuracy. (user: Dan In Philly size: 1622)

# encoding: ascii
# api: powershell
# title: Get Time Between Dates
# description: Provide a begin and end time frame (mm dd yyyy) and this will calculate the years, months and days between the two dates.  This is NOT 100% accurate but it was close enough for what I was trying to do.  Someone with better math skills should be able to improve the accuracy.
# version: 365.25
# author: Dan In Philly
# license: CC0
# x-poshcode-id: 6887
# x-archived: 2017-05-11T14:46:07
# x-published: 2017-05-05T15:41:00
#
#
Clear
$start = Read-Host "Start date (mmddyyyy)"
    $startY = $start.Substring(4,4)
    $startM = $start.Substring(0,2)
    $startD = $start.Substring(2,2)
    $startDate = $startY+"-"+$startM+"-"+$startD
$end = Read-Host "  End date (mmddyyyy)"
    $endY = $end.Substring(4,4)
    $endM = $end.Substring(0,2)
    $endD = $end.Substring(2,2)
    $endDate = $endY+"-"+$endM+"-"+$endD

$timespan = [datetime]$endDate - [datetime]$startDate

If($timespan.Days -gt 365) {[string]$yrs1 = $timespan.days/28/13}
    Else { $yr1 = $timespan.Days / 365.25
        $yrs1 = [string]$yr1 }
$yrs2 = $yrs1.split(".")
$Years = $yrs2[0]
$mnth1 = "." + $yrs2[1]
$mnth2 = [math]::Round($mnth1,5)
If($timespan.Days -gt 365) {$mnth3 = $mnth2 * 13}
    Else {$mnth3 = $mnth2 * 12}
$mnth4 = [string]$mnth3
$mnth5 = $mnth4.split(".")
$Months = $mnth5[0]
$days1 = "." + $mnth5[1]
$days2 = [math]::Round($days1,5)
If($timespan.Days -gt 365) {$Days = [math]::Truncate(($days2 * 24) - 1)}
    Else {$Days = [math]::Round($days2 * 30)}

Write-Host $years "years, "$months "months, "$days "days"