LoginSignup
0
0

More than 5 years have passed since last update.

【VBS】PowerShellを使用した日時書式変換

Last updated at Posted at 2017-12-14

はじめに

これは、Visual Basic Advent Calendar 2017の12/14日の記事となります。

VBScriptは日時の書式が、FormatDateTime 関数くらいしかありません。
FormatDateTime 関数 - MSDN

Dim strDate
'strDateに本日日付が yyyy/mm/dd 形式でセットされます。
strDate = FormatDateTime(Now, 1)
'strDateに現在の時刻が hh:mm 形式でセットされます。
strDate = FormatDateTime(Now, 4)

PowerShellを使う

.NET系なら日時書式指定文字列を使用してDataTimeオブジェクトを文字列に変換できます。
書式を指定して日時を文字列に変換する - dobon

.NET系なら、PowerShellが使えるかなと試したのがこちらです。

'出力結果 20171214
MsgBox Format("2017/12/14","yyyyMMdd") 

Function Format(DateTime, Pattern)
    Dim command, wshShell, objExec, wshShellStatus, Stream

    command = "PowerShell -Command Get-Date """ & DateTime & """ -Format """ & Pattern & """"

    Set wshShell = CreateObject("WScript.Shell")
    Set objExec = wshShell.Exec(command)
    objExec.StdIn.Close
    Format = objExec.StdOut.ReadAll

    Set objExec = Nothing
    Set wshShell = Nothing
End Function

PowerShellの画面が表示されるわー、遅いわーで使えるものでは無いです。

PowerShellは標準のカスタム書式では和暦に対応できていないようです。
PowerShell 日付のカスタム書式設定(和暦・西暦)

最後に

素直にFormat書式用の関数を作成するのがいいですね。
【VBS】Format関数の@書式を作ってみた

0
0
0

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
0
0