Skip to content

Instantly share code, notes, and snippets.

@pemo11
Created March 2, 2017 08:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pemo11/d37ee6376b9cd29558f6687e3e3cd47a to your computer and use it in GitHub Desktop.
Save pemo11/d37ee6376b9cd29558f6687e3e3cd47a to your computer and use it in GitHub Desktop.
Compares two DateTime values with Poweshell based on diferent cultures
<#
.Synopsis
Datumsvergleich
#>
function Compare-Date
{
param([Parameter(Mandatory=$true)][String]$Date1,
[CultureInfo]$CultureDate1 = (Get-Culture),
[Parameter(Mandatory=$true)][String]$Date2,
[CultureInfo]$CultureDate2 = (Get-Culture)
)
[DateTime]$DateValue1 = &{
$oldCulture = [System.Threading.Thread]::CurrentThread.CurrentCulture
[System.Threading.Thread]::CurrentThread.CurrentCulture = $CultureDate1
# Convert Date 1 using the specific culture
Get-Date -Date $Date1
[System.Threading.Thread]::CurrentThread.CurrentCulture = $oldCulture
}
[DateTime]$DateValue2 = &{
$oldCulture = [System.Threading.Thread]::CurrentThread.CurrentCulture
[System.Threading.Thread]::CurrentThread.CurrentCulture = $CultureDate2
# Convert Date 1 using the specific culture
Get-Date -Date $Date2
[System.Threading.Thread]::CurrentThread.CurrentCulture = $oldCulture
}
# Compare Years
if ($DateValue1.Year -gt $DateValue2.Year)
{
return 1
}
elseif ($DateValue1.Year -lt $DateValue2.Year)
{
return -1
}
# Compare Months
if ($DateValue1.Month -gt $DateValue2.Month)
{
return 1
}
elseif ($DateValue1.Month -lt $DateValue2.Month)
{
return -1
}
# Compare Days
if ($DateValue1.Day -gt $DateValue2.Day)
{
return 1
}
elseif ($DateValue1.Day-lt $DateValue2.Day)
{
return -1
}
return 0
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment