Last active
August 29, 2015 14:22
-
-
Save stknohg/34e6b3f86d4219fa5439 to your computer and use it in GitHub Desktop.
和暦の日付を取得するファンクション+エイリアス。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<# | |
.SYNOPSIS | |
和暦の日付を取得します。 | |
.EXAMPLE | |
Get-JapaneseDate | |
.EXAMPLE | |
Get-JapaneseDate -format "ggyy年MM月dd日" | |
.EXAMPLE | |
Get-Date -Year 2015 -Month 12 -Day 31 | Get-JapaneseDate | |
#> | |
Function Get-JapaneseDate(){ | |
[OutputType('PSCustomObject')] | |
[cmdletbinding()] | |
param( | |
[Parameter(Mandatory=$false, ValueFromPipeline=$true, Position=0)] | |
[datetime]$date = (Get-Date), | |
[Parameter(Mandatory=$false)] | |
[string]$format | |
) | |
$Culture = New-Object Globalization.CultureInfo("ja-JP"); | |
$JPCalendar = New-Object Globalization.JapaneseCalendar; | |
$Culture.DateTimeFormat.Calendar = $JPCalendar; | |
# Formatチェック | |
If($format -ne ""){ | |
return $date.ToString($format, $Culture); | |
} | |
# 独自のCustomObjectを返す | |
Write-Verbose $date.ToString("ggyy年M月d日 (dddd) HH:mm:ss", $Culture); | |
return [PSCustomObject]@{ | |
Era = $JPCalendar.GetEra($date) | |
EraName = $date.ToString("gg", $Culture) | |
Year = $JPCalendar.GetYear($date) | |
Month = $date.Month | |
Day = $date.Day | |
Date = $date.Date | |
RawValue = $date | |
} | |
} | |
New-Alias jpdate Get-JapaneseDate |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment