Getting your MRE production date with PowerShell
Posted: Wed Jul 10, 2019 12:39 am
I decided to write a small PowerShell script that will convert a Julian date code to a normal calendar date. I did this entirely for fun but thought I'd share it. To use it, Windows users can go to their Start Menu and search for "PowerShell ISE", open it, copy and paste the following code and click the green play button at the top and the code will run.
For those that are new to MREs, you can use this for MRE components as well as other modern US rations like MCWs, LRPs, RCWs, etc. A less technical option for you would be to use the converter at https://www.mreinfo.com/mres/mre-date-converter/ and you can read more about MRE/Julian date codes at https://www.mreinfo.com/mres/mre-date-codes/.
Code: Select all
# Prompt for date code
$DateCode = Read-Host "Enter Julian date code"
if ($DateCode -notmatch "\b\d{4}\b") {
throw "The date code must be four numbers long."
}
# Set min year to 1980 and the max to current year
$MinYear = 1980
$MaxYear = Get-Date -Format "yyyy"
$Years = $MinYear..$MaxYear
# Extract date and year from $DateCode
$Day = $DateCode.Substring($DateCode.Length - 3)
$Year = $DateCode.Substring(0,1)
if (($Day -gt "365") -or ($Day -eq "000")) {
throw "$Day is an invalid day of the year. It must be a number from 001 to 365."
}
# Display the date excluding the year
$DayMonth = (Get-Date -Day 1 -Month 1).Date.AddDays($day).ToString('MMMM dd')
# List the year based on the first digit of $datecode
$FinalYear = $Years | Select-String -Pattern \w*$Year\b
Write-Host "Your MRE was produced on $DayMonth of one of the following years: $FinalYear"