Zippy
Zippy

Reputation: 495

Powershell get-date format to calendar date

Is there a method,library or formatter to get the calendar serial date?

For example (for 2017) Jan 1 is 001 and Feb 2nd will be 033.

Looking to create case numbers like YY-xxxx (17-033).

Thanks

Upvotes: 0

Views: 1129

Answers (2)

Nkosi
Nkosi

Reputation: 247591

Get-Date returns a DateTime object so you should be able to do Get-Date.DayOfYear or something to that effect and just format it to suit your needs

$date = Get-Date

$caseNumber = [String]::Format("{0:yy}-{1:0##}", $date, $date.DayOfYear)

Upvotes: 0

This might work. PowerShell 3 is not my strong suit.

PS > get-date

Friday, January 27, 2017 4:06:52 PM

PS > ((get-date).DayOfYear).ToString("D3")
027

"D3" is a standard numeric format string in .NET.

This expression will return the "case number" you're looking for.

(Get-Date).ToString("yy") + '-' + ((Get-Date).DayOfYear).ToString("D3")
17-027

Get-Date | Get-Member is an education.

Upvotes: 1

Related Questions