How to Get Date Without Time in PowerShell?

One of my team members asked me about getting date without time in PowerShell. In this tutorial, I will explain how to get date without time in PowerShell with a few examples.

To get the current date without the time in PowerShell, you can use the ToString() method with the Get-Date cmdlet. Simply run the command (Get-Date).ToString("MM/dd/yyyy") to format the date in the common USA format of MM/dd/yyyy. For example, if today is September 15, 2024, the output will be 09/15/2024.

PowerShell Get-Date Without Time

When working with scripts, you often need to log dates, schedule tasks, or simply display dates without time.

The Get-Date cmdlet in PowerShell retrieves the current date and time. The basic syntax is:

Get-Date

This command returns the current date and time. However, we need to format the output to get the date without the time.

Now, let me show you different methods to achieve this.

Method 1: Using ToString() Method

One of the simplest ways to get the current date without the time is by using the ToString() method. Here’s how you can do it:

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

This command formats the date as MM/dd/yyyy, which is a common date format in the USA. For example, if today is September 15, 2024, the output will be:

09/15/2024

Method 2: Using -Format Parameter

Another efficient way to retrieve the date without the time is by using the -Format parameter with Get-Date. Here’s the syntax:

Get-Date -Format "MM/dd/yyyy"

This command achieves the same result as the previous method but is more concise.

Check out Compare Dates Without Time in PowerShell

Method 3: Using .NET DateTime Methods

PowerShell can leverage .NET methods to manipulate dates. We can use the [datetime] class to strip off the time component. Here’s an example:

[datetime]::Now.ToString("MM/dd/yyyy")

This command uses the .NET datetime class to format the current date.

Check out How to Format Date in PowerShell

Method 4: Custom Date Formatting

For more control over the date format, you can use custom date formatting. Let’s say you want to display the date as September 15, 2024. Here’s how you can do it:

(Get-Date).ToString("MMMM dd, yyyy")

This command formats the date to include the full month name, day, and year.

Here is the output you can see in the screenshot below:

PowerShell Get-Date Without Time

Get Date Without Time in PowerShell Examples

Now, let me show you some examples of using date without time in PowerShell.

Example 1: Log Dates in a Script

Imagine you’re writing a script to log daily activities for a project in New York. To keep the logs clean, you want to log the date without the time.

$logDate = (Get-Date).ToString("MM/dd/yyyy")
Add-Content -Path "C:\ProjectLogs\DailyLog.txt" -Value "Log Date: $logDate - Task Completed"

Example 2: Schedule Tasks

Suppose you’re scheduling a task to run daily at a specific time. You need to compare the current date without the time to determine if the task should run.

$currentDate = (Get-Date).ToString("MM/dd/yyyy")
$scheduledDate = "09/15/2024"

if ($currentDate -eq $scheduledDate) {
    Write-Output "Task is scheduled to run today."
}

Conclusion

In this tutorial, I have explained how to Get Date Without Time in PowerShell using different methods. I have also explained different examples of using date without time.

You may also like:

100 PowerShell cmdlets download free

100 POWERSHELL CMDLETS E-BOOK

FREE Download an eBook that contains 100 PowerShell cmdlets with complete script and examples.