Created
March 2, 2017 08:31
-
-
Save pemo11/d37ee6376b9cd29558f6687e3e3cd47a to your computer and use it in GitHub Desktop.
Compares two DateTime values with Poweshell based on diferent cultures
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
<# | |
.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