Skip to content

Instantly share code, notes, and snippets.

@vexx32
Last active June 20, 2020 03:53
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vexx32/6716c9995c56f58c264c0b7428c478e0 to your computer and use it in GitHub Desktop.
Save vexx32/6716c9995c56f58c264c0b7428c478e0 to your computer and use it in GitHub Desktop.
for Mark Kraus ♥
function Get-Age {
[CmdletBinding()]
param(
[Parameter(Mandatory)]
[ValidateScript(
{
$_ -lt [datetimeoffset]::UtcNow
}
)]
[datetimeoffset]
$Birthday
)
# Make it UTC if it ain't
$Birthday = $Birthday.ToOffset(0)
$current = [datetimeoffset]::UtcNow
$Years = $current.Year - $Birthday.Year
$Months = $current.Month - $Birthday.Month
$Days = $current.Day - $BirthDay.Day
$Hours = $current.Hour - $Birthday.Hour
$Minutes = $current.Minute - $Birthday.Minute
do {
if ($Months -lt 0) {
$Years--
$Months += 12
}
if ($Days -lt 0) {
$Months--
$Days += 30
}
if ($Hours -lt 0) {
$Days--
$Hours += 24
}
if ($Minutes -lt 0) {
$Hours--
$Minutes += 60
}
} while ($Months, $Days, $Hours, $Minutes -lt 0)
[PSCustomObject]@{
Years = $Years
Months = $Months
Days = $Days
Hours = $Hours
Minutes = $Minutes
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment