1

I'm getting information on monitors from WMI. There are properties called "WeekOfManufacture" and "YearOfManufacture". In this case it's:

Week : 24
Year : 2009

I'm trying to get the approximate date corresponding to that.

I'm doing this to get a really rough estimate:

(Get-Date -Date $YearOfManufacture/01/01).AddDays(7*$WeekOfManufacture)

But obviously the "week" doesn't necessarily always have 7 days.

What is the best approach?

The complete code I'm using right now is:

$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" 
}

which returns something like (info obfuscated):

SAM - 1234 - SycMastr - XXXX - 06/18/2009 00:00:00
SAM - 1234 - SycMastr - XXXX - 09/10/2009 00:00:00
Ansgar Wiechers
  • 193,178
  • 25
  • 254
  • 328
Rakha
  • 1,874
  • 3
  • 26
  • 60
  • 1
    You could use the .DayOfWeek property. ex. `$DateManufacture = (get-date -Date $YearOfManufacture/01/01).DayOfWeek` to determine whether or not it was a work day and/or additional properties – trebleCode Feb 28 '19 at 19:43
  • 1
    ±7 days in ten years? Who cares? – JosefZ Feb 28 '19 at 20:00

1 Answers1

1

Answer from Technet works perfectly (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
Rakha
  • 1,874
  • 3
  • 26
  • 60
  • 1
    Intriguing; the code is a PowerShell port of this C# answer: https://stackoverflow.com/a/9064954/45375, which mentions the use of Thursday as a reference point as being related to ISO 8601. – mklement0 Feb 28 '19 at 19:59