Skip to main content

How to Format a Date and Time with ToString in PowerShell

· 9 min read
Jagdish Kumawat
Founder @ Dewiride

ToString turns a date into text in the format you choose: [DateTime]::UtcNow.ToString('r') returns the current UTC time in the RFC 1123 format, such as Thu, 03 Dec 2026 16:40:19 GMT. This guide formats the current time and a fixed date with a standard format and with patterns of your own, with real output from PowerShell 7.6.10.

A date in PowerShell is a DateTime object, not text, and its ToString method returns it as text. The DateTime.ToString page describes the call with a format: "Converts the value of the current DateTime object to its equivalent string representation using the specified format and the formatting conventions of the current culture."

Syntax​

Syntax
<date>.ToString('<format>')
[DateTime]::UtcNow.ToString('<format>')
PartWhat it means
<date>A date and time, such as a variable that holds one
.ToString(...)The method that returns the date as text. The parentheses are required
'<format>'The format, in quotes: one letter for a standard format, such as r, or a pattern of letters for a format of your own, such as yyyy-MM-dd
[DateTime]::UtcNowThe current date and time in UTC

How the two kinds of format are told apart, from the Standard date and time format strings page: "Any date and time format string that contains more than one character, including white space, is interpreted as a custom date and time format string."

The Example​

The example turns the current UTC time into text, then formats one fixed date and time, 3 December 2026 at 14:30, in several ways. The commands only read the clock and make text, so they change nothing on your computer. They run in the PowerShell 7.6.10 console on a Mac.

Prerequisites​

  • PowerShell 7. This guide uses PowerShell 7.6.10: $PSVersionTable.PSVersion.ToString() printed 7.6.10.

Step 1: Get the Current UTC Time​

PowerShell
[DateTime]::UtcNow
Output
Thursday, December 3, 2026 4:40:17 PM

[DateTime] names the date type that PowerShell uses, from .NET, and ::UtcNow reads its UtcNow property. The DateTime.UtcNow page: "Gets a DateTime object that is set to the current date and time on this computer, expressed as the Coordinated Universal Time (UTC)."

The console showed the date in a long, readable form, but the value is still a date, not text. The Get-Date page: "The default formats for the output of DateTime objects are long-date and long-time formats for the currently selected locale."

Step 2: Turn the Current Time into RFC 1123 Text​

PowerShell
[DateTime]::UtcNow.ToString('r')
Output
Thu, 03 Dec 2026 16:40:19 GMT

macOS-style terminal window titled PowerShell where the UtcNow property of DateTime prints Thursday, December 3, 2026 4:40:17 PM, and the same property formatted with ToString and the r format prints Thu, 03 Dec 2026 16:40:19 GMT.

ToString('r') returned the time as text: the short day name, the day, the short month name, the year, the time on a 24-hour clock, and GMT. 16:40:19 is 4:40 PM, two seconds after the reading in Step 1, because each command read the clock when it ran.

r is a standard format: one letter that stands for the pattern ddd, dd MMM yyyy HH':'mm':'ss 'GMT'. The Standard date and time format strings page: "When this standard format specifier is used, the formatting or parsing operation always uses the invariant culture." The FAQ below shows r giving the same text with German settings.

Step 3: Format a Fixed Date Your Own Way​

PowerShell
$d = Get-Date -Date '2026-12-03 14:30:00'
$d.ToString('r')
$d.ToString('yyyy-MM-dd')
$d.ToString('dd MMM yyyy, HH:mm')

macOS-style code window titled PowerShell where Get-Date saves 3 December 2026 at 14:30 in the variable $d, and three ToString calls format it: with the r format, with the pattern yyyy-MM-dd, and with a pattern of the day, the short month name, the year, the hour and the minutes.

Output
Thu, 03 Dec 2026 14:30:00 GMT
2026-12-03
03 Dec 2026, 14:30

The first line made a date from the text and saved it in $d, without printing anything. The Get-Date page on -Date: "Specifies a date and time. Time is optional and if not specified, returns 00:00:00." A fixed date gives the same text every time, so the results are easy to compare.

The last two commands use custom formats: patterns of letters you write yourself. The Custom date and time format strings page: "A custom format string consists of one or more custom date and time format specifiers." Each specifier prints one part of the date:

SpecifierPrinted hereMeaning
yyyy2026The year, as four digits
MM12The month, as a number
MMMDecThe month's short name
dd03The day of the month, with a leading zero
HH14The hour, on a 24-hour clock
mm30The minutes

Everything else in a pattern, such as the hyphens, the comma and the spaces, appeared as typed. The page's table says of any other character: "The character is copied to the result string unchanged."

Step 4: See What ToString Does Without a Format​

PowerShell
$d.ToString()
(Get-Culture).Name
Output
12/3/2026 2:30:00 PM
en-US

With no format, ToString used this computer's culture, en-US, the one Get-Culture returned: the month first, then the day, and a 12-hour clock. The DateTime.ToString page on this form: "Converts the value of the current DateTime object to its equivalent string representation using the formatting conventions of the current culture." A computer set to another culture prints another order, so pass a format whenever the text must always look the same.

Common Mistakes​

MistakeWhat happensDo this instead
mm for the month: $d.ToString('yyyy-mm-dd')2026-30-03: the minutes, 30, took the month's placeUse capital MM for the month
hh for a 24-hour time: $d.ToString('hh:mm')02:30 for a time of 14:30Use capital HH: $d.ToString('HH:mm') printed 14:30
No parentheses: $d.ToStringA list headed OverloadDefinitions, the ways to call ToString, instead of a dateAdd the parentheses and the format: $d.ToString('r')
Text instead of a date: '2026-12-03 14:30:00'.ToString('r')MethodException: Cannot find an overload for "ToString" and the argument count: "1".Make a date first: (Get-Date -Date '2026-12-03 14:30:00').ToString('r') printed Thu, 03 Dec 2026 14:30:00 GMT

For the first two rows, the Custom date and time format strings page describes mm as "The minute, from 00 to 59." and hh as "The hour, using a 12-hour clock from 01 to 12."

FAQ​

Why UtcNow and not the local time?​

Because r doesn't convert the time it formats: it writes the time as it is and adds GMT. The Standard date and time format strings page: "Although the RFC 1123 standard expresses a time as Coordinated Universal Time (UTC), the formatting operation does not modify the value of the DateTime object that's being formatted." [DateTime]::UtcNow is already in UTC: [DateTime]::UtcNow.Kind printed Utc. The fixed date has no time zone, $d.Kind printed Unspecified, so r printed its 14:30 unchanged. For a local time, the page adds: "Therefore, you must convert the DateTime value to UTC by calling the DateTime.ToUniversalTime method before you perform the formatting operation."

Does r change with the computer's language?​

No. With the settings for German, the D format changed and r didn't:

PowerShell
$de = [cultureinfo]'de-DE'
$d.ToString('D', $de)
$d.ToString('r', $de)
Output
Donnerstag, 3. Dezember 2026
Thu, 03 Dec 2026 14:30:00 GMT

[cultureinfo]'de-DE' gets the settings for German as used in Germany, and the second argument hands them to ToString. The DateTime.ToString page on this form: "Converts the value of the current DateTime object to its equivalent string representation using the specified format and culture-specific format information."

Can Get-Date do the same?​

Yes, with -Format: Get-Date -Date $d -Format 'yyyy-MM-dd' printed 2026-12-03, and Get-Date -AsUTC -Format r printed the current UTC time, Thu, 03 Dec 2026 16:41:18 GMT. The Get-Date page: "The Format parameter outputs a String object." And on -AsUTC: "Converts the date value to the equivalent time in UTC."

Where is the RFC 1123 format used?​

In web requests. The Azure Storage page Representation of date/time values in headers calls it "the preferred format for HTTP/1.1 operations", and explains the GMT: "Both are represented in the Coordinated Universal Time (UTC), also known as Greenwich Mean Time."

Conclusion​

[DateTime]::UtcNow.ToString('r') returned the current UTC time as RFC 1123 text, Thu, 03 Dec 2026 16:40:19 GMT. Give ToString one letter for a standard format or a pattern of your own such as yyyy-MM-dd, watch the case of MM and mm, HH and hh, and format a UTC time with r.

Additional Resources​