Subtract X Days from a DateTime Object using PowerShell

Adding 10 or 100 days to a DateTime object is easy, you can simply use the AddDays() method.


PS > $Today = Get-Date
PS > $Today

Thursday, March 05, 2009 10:14:55 PM

PS > $Today.AddDays(10)

Sunday, March 15, 2009 10:14:55 PM

But how can we subtract Days??

There is a Subtract() method available through the System.DateTime Class, but we have to pass a System.TimeSpan Object to the method in order for it to work. So step one is creating a System.TimeSpan object with the amount of days that we wan to Subtract.

If we simply create a System.TimeSpan Object, all properties are default 0, and the Add() method only accept Ticks.


PS > $TimeSpan = New-Object System.TimeSPan
PS > $TimeSpan


Days              : 0
Hours             : 0
Minutes           : 0
Seconds           : 0
Milliseconds      : 0
Ticks             : 0
TotalDays         : 0
TotalHours        : 0
TotalMinutes      : 0
TotalSeconds      : 0
TotalMilliseconds : 0

PS > $TimeSpan | gm

   TypeName: System.TimeSpan

Name              MemberType Definition
----              ---------- ----------
Add               Method     System.TimeSpan Add(Ti
CompareTo         Method     System.Int32 CompareTo
Duration          Method     System.TimeSpan Durati
Equals            Method     System.Boolean Equals(
GetHashCode       Method     System.Int32 GetHashCo
GetType           Method     System.Type GetType()
Negate            Method     System.TimeSpan Negate
Subtract          Method     System.TimeSpan Subtra
ToString          Method     System.String ToString
Days              Property   System.Int32 Days {get
Hours             Property   System.Int32 Hours {ge
Milliseconds      Property   System.Int32 Milliseco
Minutes           Property   System.Int32 Minutes {
Seconds           Property   System.Int32 Seconds {
Ticks             Property   System.Int64 Ticks {ge
TotalDays         Property   System.Double TotalDay
TotalHours        Property   System.Double TotalHou
TotalMilliseconds Property   System.Double TotalMil
TotalMinutes      Property   System.Double TotalMin
TotalSeconds      Property   System.Double TotalSec

So how many Ticks are equal to 1 day ?? Don’t worry, we don’t have start calc.exe and type in various calculations to solve this. We can simply pass a few arguments to the TimeSpan object when we create it

System.TimeSpan takes a total of 5 arguments as shown below.


PS > New-Object System.TimeSpan 1, 2, 3, 4, 5


Days              : 1
Hours             : 2
Minutes           : 3
Seconds           : 4
Milliseconds      : 5
Ticks             : 937840050000
TotalDays         : 1.08546302083333
TotalHours        : 26.0511125
TotalMinutes      : 1563.06675
TotalSeconds      : 93784.005
TotalMilliseconds : 93784005

So, if we want to Add 10 Days it would be done like this.


PS > New-Object System.TimeSpan 10, 0, 0, 0, 0


Days              : 10
Hours             : 0
Minutes           : 0
Seconds           : 0
Milliseconds      : 0
Ticks             : 8640000000000
TotalDays         : 10
TotalHours        : 240
TotalMinutes      : 14400
TotalSeconds      : 864000
TotalMilliseconds : 864000000

Adding the TimeSpan of 10 days to a new object lets us subtract these days from our date.


PS > $SubtractDays = New-Object System.TimeSpan 10, 0, 0, 0, 0
PS > $Today.Subtract($SubtractDays)


Monday, February 23, 2009 10:14:55 PM

Here’s a function that takes Date and Days to subtract as an argument and returns the date.


PS > function Subtract-Days([string]$GetDate,[int]$Days) {

	$Date = Get-Date $GetDate
	$SubtractDays = New-Object System.TimeSpan $Days, 0, 0, 0, 0
	return $Date.Subtract($SubtractDays)
}

Here’s an example on running the function


PS > Subtract-Days 12/31/2005 30


Thursday, December 01, 2005 12:00:00 AM

Rating 3.50 out of 5
[?]
Categories: Date and Time Tags:
  1. JasonMArcher
    March 26th, 2009 at 00:36 | #1

    You can add negative numbers to do subtraction.

    PS > $Today = Get-Date
    PS > $Today.AddDays(-10)

    PS > $Days = 10
    PS > $Today.AddDays(-1 * $Days)

  2. James
    April 9th, 2009 at 15:01 | #2

    Cheers for this, I used a bit of of your code to convert how many Hours, Minutes and seconds there are for so many milliseconds.

    $iDuration = new-object system.timespan 0,0,0,0,87633221

    [string]$iDuration.Hours+”:”+[string]$iDuration.Minutes+”:”+[string]$iDuration.Seconds

    Thanks
    James

  3. DaveMorse
    October 14th, 2009 at 20:52 | #3

    Just wanted to say thanks. I used Jason’s method since I only needed to subtract days. Worked like a champ and is extremely simple. Nikolas’ solution is very flexible, buta solution I will use it if presented with a more complex requirement.

    Thanks,
    Dave

  4. March 19th, 2010 at 16:38 | #4

    I recently came across your blog. I must say I have enjoyed reading and I will continue to come back often. Thanks

  5. April 2nd, 2011 at 03:17 | #5

    You can also do this:

    ((get-date).adddays(-4))

  1. No trackbacks yet.

Comment Spam Protection by WP-SpamFree