Created
February 9, 2023 10:53
-
-
Save julian-wendt/3051904a6420d1c4ff5827df989e3b8a to your computer and use it in GitHub Desktop.
Function to find the closest date to a given date
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function Find-ClosestDate { | |
param( | |
[Parameter(Mandatory)] | |
[DateTime]$GivenDate, | |
[Parameter(Mandatory)] | |
[DateTime[]]$CompareDates | |
) | |
$Result = $CompareDates | Select-Object -Property Date, @{ Name = "Index"; Expression = { [math]::abs($($_.Subtract($GivenDate)).TotalSeconds) } } | | |
Sort-Object -Property Index | Select-Object -First 1 | |
if ($null -ne $Result) { | |
return $Result.Date | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment