この記事は2008/02/21にわんくまブログで書いたものです。
指定した年月の第1日曜日を取得する関数を作ってみました。
Get-FirstSunday.ps1
#=============================================================================== # Get-FirstSunday: 指定した年月の第1日曜日を取得する # # パラメータ: # $Year: 年(省略した場合は現在の年) # $Month: 月(省略した場合は現在の月) # # 使用例1(年月指定) # PS > Get-FirstSunday 2008 2 # # 使用例2(年月省略) # PS > Get-FirstSunday # # copyright HIRO's.NET(http://hiros-dot.net/) #=============================================================================== function global:Get-FirstSunday { Param ([int]$Year = $(Get-Date).Year, [int]$Month = $(Get-Date).Month) for ( $i = 0; $i -lt 7; $i++ ) { $checkDay = New-Object DateTime($Year, $Month, [int]($i + 1)) if ( $checkDay.DayOfWeek -eq 0 ) { break } } return $i + 1 }
ポイント
なんのことはありません。
DateTime構造体のDayOfWeekメソッドを使用し、指定した年月の1~7までのどれが日曜日かを判定して返します。
補足
この関数は、パラメータ年、月を省略した場合は当月の第1日曜日を返すようにしてあります。
Theme design by Jelle Druyts
Pick a theme: BlogXP business calmBlue Candid Blue dasBlog dasblogger DirectionalRedux Discreet Blog Blue Elegante essence Just Html MadsSimple Mobile Mono Movable Radio Blue Movable Radio Heat nautica022 orangeCream Portal Project84 Project84Grass Slate Sound Waves Tricoleur useit.com Voidclass2 BlogXP business calmBlue Candid Blue dasBlog dasblogger DirectionalRedux Discreet Blog Blue Elegante essence Just Html MadsSimple Mobile Mono Movable Radio Blue Movable Radio Heat nautica022 orangeCream Portal Project84 Project84Grass Slate Sound Waves Tricoleur useit.com Voidclass2
Powered by: newtelligence dasBlog 2.0.7226.0
The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.
© Copyright 2009, HIRO
E-mail