<<< Back to Search

Scripting - Get Date Time with Format (Get-Date)

Get Date and Time with common Formats

Snippet and Sample

Copy text below into a PowerShell session and execute to see the code in action.

# Scripting - Get Date Time with Format (Get-Date)
# Snippet and Sample
# www.PowerShellExamples.com

# DATE + TIME Formats

# Get Date and Time - Custom Format - Timestamp (20230228_204218) -_-
$date = get-date -format yyyyMMdd_HHmmss
Write-Host "Get Date and Time - Custom to use as Timestamp: `r`n$date`r`n"

# Get Date and Time - Sortable pattern (2023-02-28T20:45:20) -_-
$date = get-date -format s
Write-Host "Get Date and Time - Sortable pattern: `r`n$date`r`n"


# DATE only Formats

# Get Date - Short Format (2/28/2023) -_-
$date = get-date -format d
Write-Host "Get Date - Short Format: `r`n$date`r`n"

# Get Date Long Format (Tuesday, February 28, 2023) -_-
$date = get-date -format D
Write-Host "Long date Format: `r`n$date`r`n"

# Get Date - Custom Format - Long Names (Tuesday, February 28 2023) -_-
$date = get-date -format "dddd, MMMM dd yyyy"
Write-Host "Get Date - Custom Format - Long Names: `r`n$date`r`n"

# Get Date - Custom Format - Mid Names (Tue, Feb 28 2023) -_-
$date = get-date -format "ddd, MMM dd yyyy"
Write-Host "Get Date - Custom Format - Mid Names: `r`n$date`r`n"



# TIME only Formats

# Get Time - Custom Format - 24 hours (8:48 PM) -_-
$date = get-date -format "HH:mm:ss"
Write-Host "Get Time - Custom Format - 24 hours: `r`n$date`r`n"

# Get Time Short Format (8:48 PM) -_-
$date = get-date -format t
Write-Host "Short Time Format: `r`n$date`r`n"

# Get Time Long Format (8:48:05 PM) -_-
$date = get-date -format T
Write-Host "Long Time Format: `r`n$date`r`n"


    


Result

PS C:\>

Get Date and Time - Custom to use as Timestamp: 
20230228_231112

Get Date and Time - Sortable pattern:
2023-02-28T23:11:12

Get Date - Short Format:
2/28/2023

Long date Format:
Tuesday, February 28, 2023

Get Date - Custom Format - Long Names:
Tuesday, February 28 2023

Get Date - Custom Format - Mid Names:
Tue, Feb 28 2023

Get Time - Custom Format - 24 hours:
23:11:12

Short Time Format: 
11:11 PM

Long Time Format:
11:11:12 PM