How to format DateTime in PowerShell

I can format the Get-Date cmdlet without any problems:

 $date = Get-Date -format "yyyyMMdd" 

But as soon as I have a date in a variable, how do I format it? Statement below

 $dateStr = $date -format "yyyMMdd" 

returns this error:

"You must provide a value expression on the right side of the -f operator

+135
datetime powershell
Feb 12 2018-10-12T00
source share
10 answers

Same as in .NET :

 $DateStr = $Date.ToString("yyyyMMdd") 

Or:

 $DateStr = '{0:yyyyMMdd}' -f $Date 
+197
Feb 12 '10 at 4:16
source share

The question is answered, but there is no additional information:

Variable vs. Cmdlet

You have a value in the $Date variable and the -f operator works in this form: the values ​​are 'format string' -f values . If you call Get-Date -format "yyyyMMdd" you invoke the cmdlet with some parameters. The value "yyyyMMdd" is the value for the Format parameter (try help Get-Date -param Format ).

-f operator

There are many format lines. Look at least at part1 and part2 . It uses string.Format('format string', values') . Think of it as the values ​​of 'format-string' -f values , because the -f operator works very similar to the string.Format method (although there are some differences (for more information, look at the question of stack overflow: exactly how is the RHS PowerShell statement Does -f work? ).

+24
Feb 12 '10 at 5:31
source share

One thing you can do is:

 $date.ToString("yyyyMMdd") 
+18
Feb 12 2018-10-12T00
source share

A very convenient - but probably not very effective - solution is to use the GetDateTimeFormats() member function

 $d = Get-Date $d.GetDateTimeFormats() 

This outputs a large string array of formatting styles for the date value. Then you can select one of the elements of the array using [] -operator, for example,

 PS C:\> $d.GetDateTimeFormats()[12] Dienstag, 29. November 2016 19.14 
+17
Nov 29 '16 at 18:19
source share

A simple and pleasant way:

$time = (Get-Date).ToString("yyyy:MM:dd")

or simply

(Get-Date).ToString("yyyy:MM:dd")

+11
Aug 19 '16 at 19:34
source share

Do this if you absolutely need to use -Format :

 $dateStr = (Get-Date $date -Format "yyyMMdd") 

However

 $dateStr = $date.toString('yyyMMdd') 

probably more efficient .. :)

+4
May 23 '17 at 11:11
source share

If you are here to use this in DOS:

 powershell -Command (Get-Date).ToString('yyyy-MM-dd') 
+3
May 17 '17 at 10:13
source share

For those trying to format the current date for use in the HTTP header, use the "r" format (short for RFC1123), but beware of the caution ...

 PS C:\Users\Me> (get-date).toString("r") Thu, 16 May 2019 09:20:13 GMT PS C:\Users\Me> get-date -format r Thu, 16 May 2019 09:21:01 GMT PS C:\Users\Me> (get-date).ToUniversalTime().toString("r") Thu, 16 May 2019 16:21:37 GMT 

Those. don't forget to use "ToUniversalTime ()"

+2
May 16 '19 at 18:30
source share

A very informative answer from @stej , but here is a short answer: Among other options, you have 3 simple formatting options [System.DateTime] stored in a variable:

  1. Pass the variable to Get-Date : Get-Date -Format "HH:mm" $date

  2. Use toString () method : $date.ToString("HH:mm")

  3. Use Composite formatting : "{0:HH:mm}" -f $date

+1
Aug 17 '18 at 15:39
source share

I needed time and small format changes. This works great for my purposes:

 $((get-date).ToLocalTime()).ToString("yyyy-MM-dd HHmmss") 

2019-08-16 215757

0
Aug 16 '19 at 2:59
source share



All Articles