一个快速的问题,显然今天(2020年1月6日)的周数应该为2,因为2020年有53周。
但是,以下PowerShell代码段返回1:
(Get-Date -UFormat %V)
正确获取周数的好方法是什么?
答案 0 :(得分:3)
要将Get the correct week number of a given date的@il_guru C#答案转换为 PowerShell :
Function GetIso8601WeekOfYear([DateTime]$Date) {
$Day = (Get-Culture).Calendar.GetDayOfWeek($Date)
if ($Day -ge [DayOfWeek]::Monday -and $Day -le [DayOfWeek]::Wednesday) {$Date = $Date.AddDays(3)}
(Get-Culture).Calendar.GetWeekOfYear($Date, 'FirstFourDayWeek', 'Monday')
}
GetIso8601WeekOfYear (Get-Date)
2
GetIso8601WeekOfYear (Get-Date('2016-01-01'))
53