我正在从WMI获取有关监视器的信息。有称为“ WeekOfManufacture”和“ YearOfManufacture”的属性。在这种情况下,它是:
Week : 24 Year : 2009
我正在尝试获取与之相对应的大概日期。
我这样做是为了得到一个粗略的估计:
(Get-Date -Date $YearOfManufacture/01/01).AddDays(7*$WeekOfManufacture)
但是很明显,“一周”不一定总是有7天。
什么是最好的方法?
我现在正在使用的完整代码是:
$Monitors = Get-WmiObject WmiMonitorID -Namespace root\wmi
foreach ($Monitor in $Monitors) {
$Manufacturer = ($Monitor.ManufacturerName -notmatch 0 | ForEach{[char]$_}) -join ""
$Code = ($Monitor.ProductCodeID -notmatch 0 | ForEach{[char]$_}) -join ""
$Name = ($Monitor.UserFriendlyName -notmatch 0 | ForEach{[char]$_}) -join ""
$Serial = ($Monitor.SerialNumberID -notmatch 0 | ForEach{[char]$_}) -join ""
$WeekOfManufacture = $Monitor.WeekOfManufacture
$YearOfManufacture = $Monitor.YearOfManufacture
$DateManufacture = (get-date -Date $YearOfManufacture/01/01).AddDays(7*$WeekOfManufacture)
"$Manufacturer - $Code - $Name - $Serial - $DateManufacture"
}
返回类似(混淆信息)的内容:
SAM - 1234 - SycMastr - XXXX - 06/18/2009 00:00:00 SAM - 1234 - SycMastr - XXXX - 09/10/2009 00:00:00
答案 0 :(得分:1)
Technet的答案非常有效 (https://social.technet.microsoft.com/Forums/en-US/f65c80b0-f74f-4234-870c-c5ffe8d9b1ea/powershell-get-date-from-week-number-of-year?forum=ITCG) :
Function FirstDateOfWeek
{
param([int]$year, [int]$weekOfYear)
$jan1 = [DateTime]"$year-01-01"
$daysOffset = ([DayOfWeek]::Thursday - $jan1.DayOfWeek)
$firstThursday = $jan1.AddDays($daysOffset)
$calendar = ([CultureInfo]::CurrentCulture).Calendar;
$firstWeek = $calendar.GetWeekOfYear($firstThursday, [System.Globalization.CalendarWeekRule]::FirstFourDayWeek, [DayOfWeek]::Monday)
$weekNum = $weekOfYear
if($firstweek -le 1) { $weekNum -= 1 }
$result = $firstThursday.AddDays($weekNum * 7)
return $result.AddDays(-3)
}
FirstDateOfWeek -year 2009 -weekOfYear 24
8 juin 2009 00:00:00