Getting your MRE production date with PowerShell

Discussions about US MREs and other US rations
Post Reply
SentientTootsieRoll
Posts: 359
Joined: Tue Aug 02, 2016 2:26 pm
Location: Spokane, WA, US

Getting your MRE production date with PowerShell

Post by SentientTootsieRoll » 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.

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 - 1).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"
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/.
If we don't study the mistakes of the future, we're bound to repeat them for the first time.

User avatar
mikeoverhere
Posts: 195
Joined: Thu Jan 03, 2019 3:15 am
eBay name: mikeoverhere

Re: Getting your MRE production date with PowerShell

Post by mikeoverhere » Wed Jul 10, 2019 3:30 pm

I really wish we would go to a 5 digit Julian date like some other countries. The system needs updating just like computers did in Y2K.
Socrate's last words: "I drank what?"

SentientTootsieRoll
Posts: 359
Joined: Tue Aug 02, 2016 2:26 pm
Location: Spokane, WA, US

Re: Getting your MRE production date with PowerShell

Post by SentientTootsieRoll » Sat Jul 13, 2019 3:56 am

I actually didn't know that a five digit Julian date code was a thing until you mentioned it. I also agree that it should be adopted.

I got bored and decided to add the functionality to decipher a 5 digit Julian date code to that PowerShell script. I set the year to go back to 9 years ago since it's not exactly relevant to rations but you can easily change that to any year if you'd like. Here it is for anyone interested:

Code: Select all

# Convert a four or five digit Julian date code to a normal calendar date.

# Prompt for date code
$DateCode = Read-Host "Enter Julian date code"

# Check if the date code is four or five digits and extract year from $DateCode
if ($DateCode -match "\b\d{4}\b") {
  $Year = $DateCode.Substring(0,1)
}
elseif ($DateCode -match "\b\d{5}\b") {
  $Year = $DateCode.Substring(0,2)
}
else {
  throw "The date code must be four or five numbers long."
}

# Get day from $DateCode
$Day = $DateCode.Substring($DateCode.Length - 3)

# Set min year to 9 years ago and the max to current year
$MinYear = (Get-Date).AddDays(-3285).ToString('yyyy')
$MaxYear = Get-Date -Format "yyyy"
$Years = $MinYear..$MaxYear

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 - 1).ToString('MMMM dd,')

# List the year based on the first digit of $datecode
$FinalYear = $Years | Select-String -Pattern \w*$Year\b

if ($FinalYear -eq $null) {
  throw "Please choose a year between $MinYear and $MaxYear."
}

Write-Host "$DayMonth $FinalYear"
If we don't study the mistakes of the future, we're bound to repeat them for the first time.

Post Reply