Subtract X Days from a DateTime Object using PowerShell
March 5th, 2009
4 comments
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.
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 : 93784005So, 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 : 864000000Adding 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 PMHere’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
[?]
