Monday, March 31, 2008

この記事は2008/02/21にわんくまブログで書いたものです。

指定した年月の最終日を取得する関数を作成してみました。

Get-LastDay.ps1

#===============================================================================
# Get-LastDay: 指定した年月の最終日を取得する
#
# パラメータ:
#       $Year: 年(省略した場合は現在の年)
#      $Month: 月(省略した場合は現在の月)
#
# 使用例1(年月指定)
#   PS > Get-LastDay 2008 2
#
# 使用例2(年月省略)
#   PS > Get-LastDay
#
# copyright HIRO's.NET(http://hiros-dot.net/)
#===============================================================================
function global:Get-LastDay
{
  Param ([int]$Year = $(Get-Date).Year, [int]$Month =  $(Get-Date).Month)
  return [System.DateTime]::DaysInMonth($Year, $Month)
}

 

関数本体よりコメントのほうが長い....

 

ポイント

DateTime構造体のDaysInMonthは、指定した年月が持つ日数を返します。

それをreturnしているだけです...

 

補足

パラメータを省略した場合は、当月の最終日を返します。

Monday, March 31, 2008 3:44:18 AM (Tokyo Standard Time, UTC+09:00)  #    Comments [0]  |  Trackback
Comments are closed.

Theme design by Jelle Druyts

Pick a theme: