Powershell:日期时间星期几比较逻辑 [英] Powershell: Date Time day of the week comparison logic
问题描述
Date Time对象允许我们执行如下操作:
$ CurrentDate = Get-Date
$ TextDate = get-date -date02/10/2016
if($ TextDate -lt $ CurrentDate){Write-HostTrue}
else {Write-HostFalse
输出True,因为$ TextDate小于$ CurrentDate。
遵循相同的逻辑,为什么以下代码输出错误?
$ CurrentDate = Get-Date -UFormat%V
pre>
$ TextDate = Get-Date -date02/10/2016
$ TextDate = Get-Date -date $ TextDate -UFormat%V
if($ TextDate -lt $ CurrentDate){Write-HostTrue}
else {Write-HostFalse}
唯一的区别是我们正在比较一年中的一周。如果将比较改为-gt,代码将返回True。
解决方案格式化的日期是字符串,而不是整数。字符串
6
在字符串11
之后。
这将是最正确的方法:
首先,决定一年的第一个星期实际意味着什么:
$ CalendarWeekRule = [System.Globalization.CalendarWeekRule] :: FirstDay;
#$ CalendarWeekRule = [System.Globalization.CalendarWeekRule] :: FirstFourDayWeek;
#$ CalendarWeekRule = [System.Globalization.CalendarWeekRule] :: FirstFullWeek;
然后,决定一周的哪一天是一周的第一天:
$ FirstDayOfWeek = [System.DayOfWeek] ::星期日;
#$ FirstDayOfWeek = [System.DayOfWeek] ::星期一
#有一天可用
然后你可以得到正确的周号码:
$ Today =(Get-Date).Date;
$ TodayWeek = [cultureinfo] :: InvariantCulture.Calendar.GetWeekOfYear($ Today,$ CalendarWeekRule,$ FirstDayOfWeek);
$ TargetDate = Get-Date -Date2016-02-10;
$ TargetWeek = [cultureinfo] :: InvariantCulture.Calendar.GetWeekOfYear($ TargetDate,$ CalendarWeekRule,$ FirstDayOfWeek);
if($ TargetWeek -lt $ TodayWeek){$ true} else {$ false}
请注意,如果您想要一个完整的ISO 8601周,它是稍微复杂一些。
Date Time objects allow us to perform actions like this:
$CurrentDate = Get-Date $TextDate = get-date -date "02/10/2016" if ($TextDate -lt $CurrentDate){ Write-Host "True" } else{ Write-Host "False" }
This outputs "True" because $TextDate is less than $CurrentDate.
Following the same logic, why does the following code output false?
$CurrentDate = Get-Date -UFormat %V $TextDate = Get-Date -date "02/10/2016" $TextDate = Get-Date -date $TextDate -UFormat %V if ($TextDate -lt $CurrentDate){ Write-Host "True" } else{ Write-Host "False" }
The only difference is that we are comparing the week of the year. If you change the comparison to -gt, the code returns True.
解决方案Formatted dates are strings, not integers. The string
"6"
is after the string"11"
.This would be the most correct way to do this:
First, decide what the "first week of the year" actually means:
$CalendarWeekRule = [System.Globalization.CalendarWeekRule]::FirstDay; #$CalendarWeekRule = [System.Globalization.CalendarWeekRule]::FirstFourDayWeek; #$CalendarWeekRule = [System.Globalization.CalendarWeekRule]::FirstFullWeek;
Then, decide which day of the week is the first day of the week:
$FirstDayOfWeek = [System.DayOfWeek]::Sunday; #$FirstDayOfWeek = [System.DayOfWeek]::Monday; #Any day is available
Then you can get your correct week number:
$Today = (Get-Date).Date; $TodayWeek = [cultureinfo]::InvariantCulture.Calendar.GetWeekOfYear($Today, $CalendarWeekRule, $FirstDayOfWeek); $TargetDate = Get-Date -Date "2016-02-10"; $TargetWeek = [cultureinfo]::InvariantCulture.Calendar.GetWeekOfYear($TargetDate, $CalendarWeekRule, $FirstDayOfWeek); if ($TargetWeek -lt $TodayWeek) { $true } else { $false }
Note that if you want a full ISO 8601 week, it's somewhat more complicated.
这篇关于Powershell:日期时间星期几比较逻辑的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!