PowerShell:
Порівняння двох дат
Як це робити:
# Визначаємо дві дати
$date1 = Get-Date '2023-03-01'
$date2 = Get-Date '2023-03-15'
# Порівнюємо дати
if ($date1 -lt $date2) {
Write-Output "Дата1 раніше Дати2"
} elseif ($date1 -gt $date2) {
Write-Output "Дата1 пізніше Дати2"
} else {
Write-Output "Дата1 і Дата2 однакові"
}
Поглиблене вивчення:
PowerShell uses the System.DateTime
object for date operations – it’s standard since .NET. Before, we juggled timestamp conversions. Alternatives in other languages include DateTime
in C#, date
in PHP, or libraries like Joda-Time in Java.
In PowerShell, dates are compared using -lt
(less than), -gt
(greater than), and -eq
(equal to). Under the hood, it compares ticks (the number of 100-nanosecond intervals since 1 January 0001).
Earlier versions of scripting tools lacked robust date comparison features, making tasks like calculating age or finding overdue items trickier. PowerShell streamlined this with its object-oriented approach.