DateDiff
Definition: Returns the number of intervals between two
dates.
The VBScript DateDiff function is used to determine the amount of time (months, weeks, days, hours, etc.) between two date-time values. In Windows PowerShell you can perform this same kind of date arithmetic using the New-TimeSpan Cmdlet.
For example, the following command calculates the amount of time between the current date and time and 11:30 PM on December 31, 2006. Note that the Get-Date Cmdlet is used to create the two date-time values. Also note that a dollar sign ($) is used to preface each of the Get-Date commands, and each command is enclosed in parentheses. That ensures that Windows PowerShell first calculates the two date-time values, and only then uses New-TimeSpan to determine the time interval.
The command itself looks like this:
$a = New-TimeSpan $(Get-Date) $(Get-Date –month 12 -day 31 -year 2006 -hour 23 -minute 30)
When you run this command and then echo back the value of $a you should get the following (depending, of course, on the current date and time):
Days
:
109
Hours
: 3
Minutes
: 55
Seconds
: 0
Milliseconds : 0
Ticks
: 94317000000000
TotalDays :
109.163194444444
TotalHours :
2619.91666666667
TotalMinutes : 157195
TotalSeconds : 9431700
TotalMilliseconds : 9431700000
Suppose all you really care about is the number of days between the two dates. In that case, just echo back the value of the Days property:
$a.Days