As we transition into the year 2020, a new era of IT is upon us. New technologies such as virtual and augmented reality are going to revolutionize how we interact with our data and machines. Today’s methods for managing systems don’t always keep up with this rapid change which leads to major headaches when trying to manage or debug code especially if you’re using old tools like PowerShell that relies on dates being formatted in traditional ways.
The “powershell format date dd/mm/yyyy” is a command that displays the current system time and date. This article will explain how to use this command in PowerShell.
PowerShell can accomplish a lot of things, and it can do just about whatever you want with dates, just like any decent programming language. You can get today’s date, tomorrow’s date, format dates, and a lot more with the PowerShell Get-Date command and other ways.
This post will teach you all you need to know about dates and PowerShell.
Please make sure you have the following requirements in order to follow along with the examples in this article:
- Windows PowerShell v5 or above is required. Even if you’re using an older version, all of the examples should still function.
Using PowerShell to get the current date
The Get-Date cmdlet is one of the simplest methods to get the current date in PowerShell. As illustrated below, this command shows the current date and time.
PS> Get-Date Sunday, November 22, 2020 1:25:57 AM
Get-Date seems to merely provide the current date and time by default, but in truth, it offers a lot more information. Pipe the output to the Format-List cmdlet to get this information, as seen below.
PS> Get-Date | Format-List DisplayHint : DateTime Date : 11/22/2020 12:00:00 AM Day : 22 DayOfWeek : Sunday DayOfYear : 327 Hour : 1 Kind : Local Millisecond : 163 Minute : 26 Month : 11 Second : 2 Ticks : 637416051621635773 TimeOfDay : 01:26:02.1635773 Year : 2020 DateTime : Sunday, November 22, 2020 1:26:02 AM
You may also execute Get-Date | Get-Member -MemberType Properties to retrieve all of the object properties using the Get-Member cmdlet.
If you look at the kind of object returned by Get-Date, you’ll see that it’s a System. DateTime is a kind of object. All of the properties and methods you see are exposed by this class. You may find out what sort of object it is by using Get-Member or GetType(), as illustrated below.
PS> (Get-Date).GetType().FullName System.DateTime
After you’ve seen all of the accessible attributes, you may use dot notation to refer to them, as demonstrated below.
PS> (Get-Date).Year 2020 PS> (Get-Date).DayOfWeek Sunday PS> (Get-Date).Month 11 PS> (Get-Date).DayOfYear 327
Do you find dot notation to be a little too “devvy” for you? If that’s the case, you may accomplish the same thing by using the DisplayHint argument.
PS> Get-Date -DisplayHint Date Sunday, November 22, 2020 PS> Get-Date -DisplayHint Time 2:29:35 AM PS> Get-Date -DisplayHint DateTime Sunday, November 22, 2020 2:29:39 AM
The easiest approach to retrieve the current date and other properties is to use Get-Date, but you can do so much more!
PowerShell Date Calculation
Let’s imagine you need to know the date and/or time in the past or future by a particular number of days, years, or minutes. Methods may be used to locate this information.
Methods for Dates and Times in Action
The System is in place. Get-Date produces a DateTime object with several methods for adding and removing time chunks. When you run Get-Date | Get-Member, you’ll see that there are many methods that begin with Add.
AddDays is a datetime method that may be used to add days to a Days Added (double value) Datetime AddHours Method HoursToBeAdded (double value) Method datetime AddMilliseconds AddMilliseconds is a function that adds milliseconds to (double value) Method datetimeAddMinutesAddMinutesAddMinutesAddMinutesAddMin Minutes Added (double value) Datetime AddMonths Method Months to Add (int months) Method datetimeAddSecondsAddSecondsAddSecondsAddSecon AddSeconds is a command that adds seconds to a sentence (double value) AddTicks is a datetime method that adds ticks to a list. TicksAdd (long value) AddYears is a datetime method that adds years to a date. YearAddition (int value)
Each of these approaches may be used to discover a date or time in the future or in the past. A few instances of calling each of these methods, as well as their output, are shown below.
#Adding 8 days to the current date PS> (Get-Date).AddDays(8) Monday, November 30, 2020 1:59:39 AM #Adding 3 hours to the current time PS> (Get-Date).AddHours(3) Sunday, November 22, 2020 4:59:51 AM #Adding five years to the current date PS> (Get-Date).AddYears(5) Saturday, November 22, 2025 2:00:11 AM #Subtracting 8 days from the current date using a negative number. PS> (Get-Date).AddDays(-8) Saturday, November 14, 2020 2:02:43 AM
Using the New-Timespan method
What if you wanted to know the difference between two dates rather than just adding a certain amount of time chunks? The New-Timespan cmdlet is one method to do this. The TimeSpan objects created by the New-Timespan cmdlet reflect date/time discrepancies or spans.
By passing a Start and End date/time as inputs to the New-TimeSpan cmdlet, you may create a new time span. For example, you might use the following code snippet to determine the difference between November 23rd, 2020 at midnight and December 31st, 2020 at midnight.
You can detect date/time discrepancies in just about any increment you want using TimeSpan objects.
PS> $StartDate = Get-Date -Month 11 -Day 23 -Year 2020 -Hour 00 -Minute 00 -Second 00 PS> $EndDate = Get-Date -Month 12 -Day 31 -Year 2020 -Hour 00 -Minute 00 -Second 00 PS> $StartDate Monday, November 23, 2020 12:00:00 AM PS> $EndDate Thursday, December 31, 2020 12:00:00 AM PS> New-TimeSpan -Start $StartDate -End $EndDate Days : 38 Hours : 0 Minutes : 0 Seconds : 0 Milliseconds : 672 Ticks : 32832006722080 TotalDays : 38.0000077801852 TotalHours : 912.000186724444 TotalMinutes : 54720.0112034667 TotalSeconds : 3283200.672208 TotalMilliseconds : 3283200672.208
Using Dates to Compare
You may compare dates and times using normal PowerShell operators in addition to finding discrepancies. When a date is “less than” (earlier than) or “greater than” (later than) another date, PowerShell recognizes it.
To compare dates, just use Get-Date or [DateTime] to construct two DateTime objects, then use basic PowerShell operators like lt or gt to compare them.
You can see a simple example of Using Dates to Compare below.
#Declaring the date PS> $Date1 = (Get-Date -Month 11 -Day 14 -Year 2020) PS> $Date2 = Get-Date PS> $Date1 -lt $Date2 True
Converting Strings to DateTime Objects (Casting)
Dates may be shown in a variety of forms using PowerShell. Let’s imagine you have a CSV file or spreadsheet with several dates set in the date time utc column in the way shown below. The current format is YYYY-MM-DDThh:mm:ss.0000000Z.
You want to parse this file and locate all of the rows having a date of at least seven days.
Example of a Date Format
By default, PowerShell does not recognize that 2020-09-07T13:35:08.4780000Z as a date and time; it treats it as a plain string. To make it understandable to PowerShell, you must first convert it to a DateTime object, like Get-Date does.
Prefix a text (or variable) with [DateTime] to convert it to a DateTime object. When you do this, PowerShell attempts to interpret the string as a date and time, and then gives you access to all of the object type’s properties and functions.
PS> $Date = “2020-09-07T13:35:08.4780000Z” PS> [DateTime]$Date Monday, September 07, 2020 1:35:08 PM
Date Formatting in PowerShell
Now that you know how to use dates in fundamental ways, it’s time to go further into the subject. Let’s experiment with various date formats to see what we can come up with.
You’ll get an output like Monday, September 07, 2020 1:35:08 PM whenever you run Get-Date. Instead of a simple text, this output is a DateTime object containing properties and methods. However, you may convert this DateTime object to a string and use other date formatting techniques to express it in different ways.
Methods for Dates and Times
Using methods on the DateTime object is one technique to customize how a DateTime object is presented. You may modify the formatting of the DateTime object using four methods:
The ToShortDateString() and ToShortTimeString() methods are used in the example below.
PS> [DateTime]$Date = $Date PS> $date.ToShortDateString() + ” ” + $date.ToShortTimeString() 9/7/2020 7:05 PM
The Format Parameter is a variable that determines how the data is
One way to change the date format of a DateTime object is by using Get-Date to generate the object and The Format Parameter is a variable that determines how the data is to change the format. The Format Parameter is a variable that determines how the data is accepts a string of characters each representing how a date/time string should look.
You can see a few examples of the characters you can use with The Format Parameter is a variable that determines how the data is.
#To display full date pattern with short time pattern PS> Get-Date -Format f Sunday, November 22, 2020 4:26 AM #To display full date pattern with long time pattern PS> Get-Date -Format F Sunday, November 22, 2020 4:26:31 AM #To display short date pattern PS> Get-Date -Format d 11/22/2020 #To display long date pattern PS> Get-Date -Format D Sunday, November 22, 2020 #Year month pattern PS> Get-Date -Format Y November 2020 ## Combining format characters PS> Get-Date -Format “dddd dd-MM-yyyy HH:mm K” Sunday 22-11-2020 03:41 +05:30
Check out the Microsoft documentation for a full breakdown of all the characters you can use with The Format Parameter is a variable that determines how the data is.
You’ll see as soon as you use The Format Parameter is a variable that determines how the data is, Get-Date no longer returns a DateTime object but now strings a string.
PS> (Get-Date -Format D).GetType().FullName System.String
The UFormat parameter is used to specify the format of a file.
Similar to The Format Parameter is a variable that determines how the data is, you also have a UFormat parameter with the Get-Date cmdlet. The UFormat parameter is used to specify the format of a file. allows you to define date formats by using UNIX date formatting.
The UFormat parameter is used to specify the format of a file. is similar to The Format Parameter is a variable that determines how the data is in that you can combine date format characters to change how the date is returned as a string. For example, try out the following formats and see what’s returned.
PS> Get-Date -UFormat %A PS> Get-Date -UFormat %b PS> Get-Date -UFormat %B
These date format characters may also be combined. The d, m, and Y letters are used to return the date in the format dd-mm-yyyyy, while the r character is used to provide the time in 12-hour format using the time zone t character.
PS> Get-Date -UFormat “%d-%m-%Y %r %Z” 22-11-2020 06:10:55 PM +05
Check out the Microsoft documentation for a full breakdown of all the characters you can use with The UFormat parameter is used to specify the format of a file..
The “powershell get-date minus 1 day” is a PowerShell command that returns the current date and time. This command can be used to calculate dates in different formats.
Frequently Asked Questions
How do I format a DateTime object in PowerShell?
A: To format a date time object in PowerShell, you need to use the Format-Time command. You can also do this by using some keywords in the DateTime object. For example, if I wanted to change it from 9/10/2018 12:34 PM into September 10th at 12:34PM then thats what I would type.
To start off with though let me just show you how to get your current system time and then convert that into text so we can see which commands exist for formatting dates
Set-Location C:\Windows\system32 dir | out-file C:\temp\sysdatetxt -encoding ASCII
How do I change the date format in PowerShell?
A: To change the date format, you can use these commands.
How do I convert DateTime in PowerShell?
A: The date and time are stored in the format yyyy-mm-dd hh:nn. If you want to convert that into a string with an arbitrary separator, you can use the following commands from PowerShells Convert::ToString method and then separate them as desired before returning it back to PowerShell.
- powershell datetime format
- powershell format date string
- powershell get-date format yyyymmddhhmmss
- powershell datetime object
- powershell compare dates