Reputation: 58
When I run the below two PowerShell statements, I expect them to return the same result, but in fact they return different results:
New-TimeSpan $(Get-Date) $(Get-Date -month 12 -day 31 -year 2016)
New-TimeSpan $(Get-Date) $(Get-Date "2016-12-31")
The results are:
Days : 751
Hours : 0
Minutes : 0
Seconds : 0
Milliseconds : 1
Ticks : 648864000010001
TotalDays : 751.000000011575
TotalHours : 18024.0000002778
TotalMinutes : 1081440.00001667
TotalSeconds : 64886400.0010001
TotalMilliseconds : 64886400001.0001
Days : 750
Hours : 6
Minutes : 50
Seconds : 13
Milliseconds : 67
Ticks : 648246130673175
TotalDays : 750.284873464323
TotalHours : 18006.8369631437
TotalMinutes : 1080410.21778863
TotalSeconds : 64824613.0673175
TotalMilliseconds : 64824613067.3175
What's going on?
Upvotes: 1
Views: 467
Reputation: 46700
I'm pretty sure the answer is in the different parameter sets you are using. Referring to the TechNet article. There are two different parameter sets: net
and uformat
Get-Date "2016-12-31"
is is being detected as net
(Get-Date -month 12 -day 31 -year 2016)
is being detected as uformat
In the latter, since you are not specifying the time components, it is defaulting them to the current time. Looking at the documentation for -Hour
-Hour<Int32>
Specifies the hour that is displayed. Enter a value from 1 to 23. The default is the current hour.
When the string "2016-12-31" is cast to a datetime midnight, it is used in the absence of a value. In the other case no time is specified, so it is using the current time in its place.
PS C:\Users\Cameron> (Get-Date -month 12 -day 31 -year 2016)
Saturday, December 31, 2016 5:14:32 PM
PS C:\Users\Cameron> $(Get-Date "2016-12-31")
Saturday, December 31, 2016 12:00:00 AM
I might be slightly wrong on the terms or cause, but I have to be close. Even when specifying hour for example the other time values still need to come from somewhere. The time when the code was run was 5:24 pm EST.
PS C:\Users\Cameron> (Get-Date -month 12 -day 31 -year 2016 -Hour 12)
Saturday, December 31, 2016 12:24:43 PM
Upvotes: 3