New-TimeSpan of two midnight dates skips every other day

Viewed 284

I am attempting to simply calculate the number of days until a user's password expires. I use a TimeSpan to do this. My code in practice is as follows:

(New-TimeSpan -Start (get-date -hour 0 -minute 0 -second 0) -End (Get-Date $_.PasswordLastSet -hour 0 -minute 0 -second 0).AddDays(90)).Days

So if the current date is 90 days after the date when the password was last set, the days until password expiration would be 0.

Unfortunately, this code will return the same result for many consecutive days. For some reason I can't seem to get completely consistent results here, I assume it depends on the time of day, but am not sure why. Most of the time, we skip every other day. So if we ran this every day of the week, we would return 4,4,2,2,0,0.

To easily replicate, you can use the following:

$x = (get-date).AddDays(-90)
(New-TimeSpan -Start (get-date $x -hour 0 -minute 0 -second 0).AddDays(90) -End (get-date -hour 0 -minute 0 -second 0)).Days
(New-TimeSpan -Start (get-date $x -hour 0 -minute 0 -second 0).AddDays(89) -End (get-date -hour 0 -minute 0 -second 0)).Days
2 Answers
Related