Skip to content

Instantly share code, notes, and snippets.

@stknohg
Last active August 29, 2015 14:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save stknohg/34e6b3f86d4219fa5439 to your computer and use it in GitHub Desktop.
Save stknohg/34e6b3f86d4219fa5439 to your computer and use it in GitHub Desktop.
和暦の日付を取得するファンクション+エイリアス。
<#
.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