Skip to content

Instantly share code, notes, and snippets.

@julian-wendt
Created February 9, 2023 10:53
Show Gist options
  • Save julian-wendt/3051904a6420d1c4ff5827df989e3b8a to your computer and use it in GitHub Desktop.
Save julian-wendt/3051904a6420d1c4ff5827df989e3b8a to your computer and use it in GitHub Desktop.
Function to find the closest date to a given date
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