Categories
Software development

Powershell Date math

Since we know that the Get-Date command outputs a DateTime object, is there more that we can do with this object? Not that we will go into every method, but there are a few methods that are very useful to use. The following are a handful of functions that are available for use with an existing DateTime objects. You can use either the -Format parameter or the -UFormat parameter. The latter parameter uses the Unix formatting syntax whereas the former uses .NET formatting syntax. In the below code example we demonstrate an example using both.

Create a free account today to participate in forum conversations, comment on posts and more. There are a handful of static functions that are very useful as well. 2699 PowerShell says “execution of scripts is disabled on this system.”

Just like with the date format, we can choose between .Net format and UFormat specifiers to format the DateTime result of Get-Date in PowerShell. Thus, the article covered in detail in the various date time formats along with examples. The best way to learn about each format available is to try out each format and understand which is suitable for the script need. You’ll see as soon as you use the Format parameter, Get-Date no longer returns a DateTime object but now strings a string. Use the New-TimeSpan cmdlet by providing a Start and End date/time as parameters. For example, if you’d like to find the difference between November 23rd, 2020 at midnight to December 31st, 2020 at midnight, you could use the following code snippet.

Instead, you should use the DateTime objects themselves to perform the comparison.

powershell date math

Working with dates may seem difficult at first, but I have to say, PowerShell made it pretty easy. As you have seen we can easily compare dates, add or subtract date and time from dates, and calculate the date and time between two dates. There are many ways to work with dates and times in PowerShell and there are even more methods and techniques that can take your script writing to the next level. Leverage the extensive power of Get-Date, New-TimeSpan, and the DateTime object to create advanced scripts that handle dates and times with ease.

Recommended Articles

To do this we are going to use the New-TimeSpan cmdlet with two date objects. The first method simply returns the number of days in any given month when passed a year and month number. Parse attempts to create a DateTime object out of a given string using common formats. What if the string you are passing in doesn’t fit any known format?

powershell date math

To cast a string to a DateTime object, preface the string with . When you do this, PowerShell tries to interpret the string as a date and time then provides you with all of the properties and methods available on this object type. With Get-Date we can get the current date and time which is often used in PowerShell scripts to timestamp the output or in combination with logging. https://bitcoin-mining.biz/ But besides timestamping, we can also use dates to compare or calculate the age of files, or in filters to select only a specific range from a result set. The System.DateTime object that PowerShell Get Date command returns has various methods you can invoke to add or remove chunks of time. If you run Get-Date | Get-Member, you’ll see various methods that start with Add.

By default, PowerShell Get Date command looks like it only returns the current date and time but, in reality, it’s actually returning a lot more information. To find this information pipe the output to the Format-List cmdlet as shown below. The UFormat parameter is similar to the Format parameter 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. Now that you are aware of the basic methods of using dates, it’s time to delve deeper into the topic.

There are many different formatting identifiers available, and we will not list them all here but can be referenced here. To use this, let us show the example of adding two days and five hours to a date. We can chain the AddDays and the Addhours methods onto our date stored in the variable $CurrentDate. You have half the hard work done, converting the information/strings into DateTime objects.

Comparing Dates

This class is what exposes all of the different properties and methods you see. You can discover it’s object type by using Get-Member or using the GetType() as shown below. How many times have you had to figure out what date was X days, months, or years ago, or perhaps what time was Y minutes, hours, or seconds ago? Producing a report of all the users who have not logged in during the past 90 days is a pretty common request. It’s very easy to calculate date and time math with Windows PowerShell. The .NET Framework includes two data structures that you can use for calculating all sorts of date math – DateTime and TimeSpan.

  • PowerShell provides a number of cmdlets to retrieve current date time and to create time span object.
  • There are many different formatting identifiers available, and we will not list them all here but can be referenced here.
  • The DateTime objects can be compared directly with each other using the normal PowerShell comparison operators -gt and -lt for example.
  • Comparing dates in PowerShell is actually easier than you might think.

PowerShell can do a lot of things and, like any good programming language, it can do just about anything you want with dates. Using the PowerShell Get Date command and other techniques, you can find today’s date, tomorrow’s date, format dates, and a whole lot more. To specify a start and end date , then you can determine the number of days until that point. Demonstrate using the same format for both .NET and Unix formatting strings in Get-Date. # get the number days in the month, so we can get a denominator when figuring the percent of the way we are towards the next anniversay. Check out the Microsoft documentation for a full breakdown of all the characters you can use with the UFormat parameter.

Formatting Dates and Times

Now you only need to remove the formatting portion in the if statement to make sure you don’t convert back into strings. Before you can work with dates in PowerShell, you will need to make sure that the dates are DateTime objects. For example, when you have a CSV file with dates in it, that you would like to use. To format the DateTime in PowerShell using the UFormat specifier, you will need to use the parameter -Uformat followed by a string with the format. You can find all options for UFormat here in the documentation.

Let’s say that you need to know the date and/or time a certain number of days, years, or minutes in the past or in the future. Similar to the Format parameter, you also have a UFormat parameter with the Get-Date cmdlet. The UFormat parameter allows you to define date formats by using UNIX date formatting. You can call each of these methods to find a date/time in the future or in the past. Below you’ll see a few examples of invoking each of these methods and their output.

Finding the Current Date with PowerShell

In both cases, the output is a string object and not a date object. One way to change the date format of a DateTime object is by using Get-Date to generate the object and the Format parameter to change the format. The Format parameter accepts a string of characters each representing how a date/time string should look. Not only can you find differences in dates and times but you can also compare them using standard PowerShell operators.

PowerShell knows when a date is “less than” or “greater than” another date. What if you wanted to know the difference between two dates; not just adding a specific number of time chunks? The New-Timespan cmdlet generates TimeSpan objects which represent date/time differences or spans.

You can use ParseExact to do so by giving a date and time template to follow. Of course, we often want to perform a date calculation from the current time. How would we go about subtracting or adding time to the current date? With the DateTime object type, there are several built-in methods that make this very easy (non-exhaustive list below).

Don’t worry about the date format until you want to display it or read it from a file or string. Get-Date returns a DateTime object that can be compared directly with another DateTime Career in the FinTech industry object without regard for culture/formatting. We can compare dates, add, or subtract days from dates, but did you know that we can also calculate the days between dates in PowerShell?

You can see a few examples of the characters you can use with the Format parameter. You’d like to parse this file and find all of the rows with a date seven days or older. #Subtracting 8 days from the current date using a negative number. As you can see, Thursday is now returned in the Dutch language using the nl-NL encoding. Connect and share knowledge within a single location that is structured and easy to search. I hope you found this article helpful, if you have any questions, just drop a comment below.