このエントリーをはてなブックマークに追加


Get-Dateコマンドレットで前日、翌日、前月、翌月などの日付を取得する方法

Get-Dateコマンドレットで前日、翌日、前月、翌月などの日付を取得する方法を以下に記します。


関連資料

使用するメソッド

Get-Dateの以下のメソッドを利用します。

PS C:\Users\sakura> Get-Date | Get-Member


   TypeName: System.DateTime

Name                 MemberType     Definition
----                 ----------     ----------
<snip>
AddDays              Method         System.DateTime AddDays(double value)
<snip>
AddMonths            Method         System.DateTime AddMonths(int months)
<snip>
AddYears             Method         System.DateTime AddYears(int value)
<snip>

前日、翌日を取得

AddDaysメソッドを利用して前日、翌日を求めます。

PS C:\Users\sakura> Get-Date

2012年5月23日 17:02:23


PS C:\Users\sakura> (Get-Date).AddDays(-1)

2012年5月22日 17:02:33


PS C:\Users\sakura> (Get-Date).AddDays(1)

2012年5月24日 17:02:42

先月、翌月を取得

AddMonthsメソッドを利用して先月、翌月を求めます。

PS C:\Users\sakura> Get-Date

2012年5月23日 17:04:08


PS C:\Users\sakura> (Get-Date).AddMonths(-1)

2012年4月23日 17:04:14


PS C:\Users\sakura> (Get-Date).AddMonths(1)

2012年6月23日 17:04:17

前年、翌年を取得

AddYearsメソッドを利用して前年、翌年を求めます。

PS C:\Users\sakura> Get-Date

2012年5月23日 17:05:11


PS C:\Users\sakura> (Get-Date).AddYears(-1)

2011年5月23日 17:05:20


PS C:\Users\sakura> (Get-Date).AddYears(1)

2013年5月23日 17:05:27


トップ   編集 凍結 差分 バックアップ 添付 複製 名前変更 リロード   新規 一覧 検索 最終更新   ヘルプ   最終更新のRSS
Last-modified: 2016-02-16 (火) 22:56:29