LoginSignup
2
8

More than 3 years have passed since last update.

PowerShellで日付を扱う

Last updated at Posted at 2020-04-29

データ型 書式 加減算

標準書式(Datetime型-String型間の双方向の変換可)

Datetime型から文字列

# 2019/12/31 という文字列を取得する(現在日時)
PS C:\Users\hidemaru> $DATE = Get-Date -Format "d" # -> 2019/12/31
2019/12/31

PS C:\Users\hidemaru> $DATE.GetType()
IsPublic IsSerial Name                                     BaseType
-------- -------- ----                                     --------
True     True     String                                   System.Object

文字列からDatetime型

# 2019/12/31 という文字列からDatetime型で取得する
PS C:\Users\hidemaru> $DATE =[Datetime]"2019/12/31"
PS C:\Users\hidemaru> $DATE
20191231 0:00:00

PS C:\Users\hidemaru> $DATE.GetType()

IsPublic IsSerial Name                                     BaseType
-------- -------- ----                                     --------
True     True     DateTime                                 System.ValueType

標準書式の形式のパターン

  • 2019/12/31 ( $DATE.ToShortDateString() or $DATE.ToString("d") )
  • 2019/12/31 0:00
  • 2019/12/31 0:00:00
  • 2019年12月31日 ( $DATE.ToLongDateString() or $DATE.ToString("D") )
  • 2019年12月31日 0:00
  • 2019年12月31日 0:00:00

出典:Standard date and time format strings

カスタム書式(Datetime型からString型へのみ)

@nukie_53 さんよりコメ欄にて
カスタム書式のString型からDatetime型への変換方法をご紹介いただきたました。
https://qiita.com/hidemaru/items/5c60adf45f746aa40dd1#comment-5ac7fdad2356d083d9ee

書式文字を複数文字使った場合に、カスタム書式になる。

# 20191231 という文字列を取得する(現在日時)
PS C:\Users\hidemaru> $DATE = Get-Date -Format "yyyyMMdd" # -> 20191231
PS C:\Users\hidemaru> $DATE.GetType()                                                                                   
IsPublic IsSerial Name                                     BaseType
-------- -------- ----                                     --------
True     True     String                                   System.Object

※秒(ss)、ナノ秒(fffffffff)
※書式文字列は .Net のフォーマットに同じ
Custom date and time format strings

日付の計算

年、月、日 を足す

# 2019/12/31 に 1年足す
PS C:\Users\hidemaru> $DATE.AddYears(1)                                                                                 
20201231 0:00:00

# 2019/12/31 に 1月足す
PS C:\Users\hidemaru> $DATE.AddMonths(1)                                                                                
2020131 0:00:00

# 2019/12/31 に 1日足す
$DATE = Get-Date # -> 2019/12/31
PS C:\Users\hidemaru> $DATE.AddDays(1)
202011 0:00:00

日付から日付を引く(日付の差分)

# 2019/12/31 から 2020/01/01 の期間(1日前)
PS C:\Users\hidehiko> $DATE.Subtract([Datetime]"2020/01/01")                                                            

Days              : -1
Hours             : 0
Minutes           : 0
Seconds           : 0
Milliseconds      : 0
Ticks             : -864000000000
TotalDays         : -1
TotalHours        : -24
TotalMinutes      : -1440
TotalSeconds      : -86400
TotalMilliseconds : -86400000

戻り値の方は以下を参照。
出典:TimeSpan 構造体

リンク

Get-Date リファレンス

2
8
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
8