<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>PowerShell.nu &#187; Date and Time</title>
	<atom:link href="http://www.powershell.nu/tag/date-and-time/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.powershell.nu</link>
	<description></description>
	<lastBuildDate>Wed, 14 Jul 2010 22:17:44 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>Subtract X Days from a DateTime Object using PowerShell</title>
		<link>http://www.powershell.nu/2009/03/05/subtract-x-days-from-a-datetime-object-using-powershell/</link>
		<comments>http://www.powershell.nu/2009/03/05/subtract-x-days-from-a-datetime-object-using-powershell/#comments</comments>
		<pubDate>Thu, 05 Mar 2009 21:45:56 +0000</pubDate>
		<dc:creator>Niklas Goude</dc:creator>
				<category><![CDATA[Date and Time]]></category>

		<guid isPermaLink="false">http://www.powershell.nu/?p=504</guid>
		<description><![CDATA[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 [...]]]></description>
			<content:encoded><![CDATA[<p>Adding 10 or 100 days to a DateTime object is easy, you can simply use the AddDays() method.</p>
<p />
<pre>
<strong>
PS > $Today = Get-Date
PS > $Today
</strong>
Thursday, March 05, 2009 10:14:55 PM

<strong>PS > $Today.AddDays(10)</strong>

Sunday, March 15, 2009 10:14:55 PM
</pre>
<p />
<p>But how can we subtract Days??<br />
<br />
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.</p>
<p />
If we simply create a System.TimeSpan Object, all properties are default 0, and the Add() method only accept Ticks.</p>
<pre>
<strong>
PS > $TimeSpan = New-Object System.TimeSPan
PS > $TimeSpan
</strong>

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

<strong>PS > $TimeSpan | gm</strong>

   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
</pre>
<p />
So how many Ticks are equal to 1 day ?? Don&#8217;t worry, we don&#8217;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</p>
<p />
System.TimeSpan takes a total of 5 arguments as shown below.</p>
<pre>
<strong>
PS > New-Object System.TimeSpan 1, 2, 3, 4, 5
</strong>

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
</pre>
<p />
So, if we want to Add 10 Days it would be done like this.</p>
<p />
<pre>
<strong>
PS > New-Object System.TimeSpan 10, 0, 0, 0, 0
</strong>

Days              : 10
Hours             : 0
Minutes           : 0
Seconds           : 0
Milliseconds      : 0
Ticks             : 8640000000000
TotalDays         : 10
TotalHours        : 240
TotalMinutes      : 14400
TotalSeconds      : 864000
TotalMilliseconds : 864000000
</pre>
<p />
Adding the TimeSpan of 10 days to a new object lets us subtract these days from our date.</p>
<p />
<pre>
<strong>
PS > $SubtractDays = New-Object System.TimeSpan 10, 0, 0, 0, 0
PS > $Today.Subtract($SubtractDays)
</strong>

Monday, February 23, 2009 10:14:55 PM
</pre>
<p />
Here&#8217;s a function that takes Date and Days to subtract as an argument and returns the date.</p>
<p />
<pre>
<strong>
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)
}
</strong>
</pre>
<p />
Here&#8217;s an example on running the function</p>
<p />
<pre>
<strong>
PS > Subtract-Days 12/31/2005 30
</strong>

Thursday, December 01, 2005 12:00:00 AM
</pre>
<p />
]]></content:encoded>
			<wfw:commentRss>http://www.powershell.nu/2009/03/05/subtract-x-days-from-a-datetime-object-using-powershell/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
	</channel>
</rss>
