Table of Contents
By default, the Get-Date cmdlet in PowerShell returns the current date and time. Sometimes, you may need a [System.DateTime] object that contains the date without the time portion.
PS C:\> Get-Date
Thursday, June 6, 2024 6:15:47 PM
If you would like to only retrieve the date without the time, you can use one of the following methods to do so in PowerShell.
Use .ToString()
The first way to get the current date without the time in PowerShell is by using the ToString() method to simply format the current date and time using M/d/yyyy as the date format:
PS C:\> (Get-Date).ToString("M/d/yyyy")
6/6/2024
Note that the format M/d/yyyy specifies that only one digit should be used for the month and day if they month and day only contain a single digit.
If you would like to display two digits for the month and day, you can use the MM/dd/yyyy format instead:
PS C:\> (Get-Date).ToString("MM/dd/yyyy")
06/06/2024
This method is especially helpful if the date and time is stored in a variable.
PS C:\> $date = Get-Date
PS C:\> $date.ToString('MM/dd/yyyy')
06/06/2024
Formatting the Date
If you need to format the date, PowerShell allows you to specify the format using the -Format parameter. It’s a string representation of the date, without any time information.
PS C:\> Get-Date -Format "MM/dd/yyyy"
06/06/2024
PS C:\> Get-Date -Format "MM/dd/yyyy" | Get-Member
TypeName: System.String
Use (Get-Date).Date
Another way to get the current date without the time in PowerShell is by using the .Date property of the Get-Date cmdlet.
PS C:\> Get-Date
Thursday, June 6, 2024 6:25:03 PM
PS C:\> (Get-Date).Date
Thursday, June 6, 2024 12:00:00 AM
This command will create a [System.DateTime] object with the time set to 00:00:00, effectively removing the time part but still leaving you with a DateTime object that can be used in date calculations or comparisons.
PS C:\> (Get-Date).Date | Get-Member
TypeName: System.DateTime
Conclusion
Using the Get-Date cmdlet in PowerShell to retrieve the current date without the time is straightforward. Here, I have explained different methods of how to get-date without time in PowerShell.
Not a reader? Watch this related video tutorial: