Answered by:
date time difference

Question
-
Hi,
I am trying to get the date and time difference between two dates. I get a datetime value from the other part of the script and need to get the date and time difference with system current time. I am trying the following code but it's erroring out "New-TimeSpan : Cannot bind parameter End. Cannot convert value 072017 20:50:20 to type System.DateTime. Error: String was not recognized as a valid DateTime."
------------------
$Time1 = "072017 15:30:00"
$Time2 = Get-Date -format "MMddyy HH:mm:ss"
$TimeDiff = New-TimeSpan $Time1 $Time2
$TimeDiff
- Edited by cloudify Thursday, July 20, 2017 6:54 PM
Thursday, July 20, 2017 6:53 PM
Answers
-
You can't subtract strings.
$date1 = [datetime]::ParseExact('072017 15:30:00','MMddyy HH:mm:ss',[cultureinfo]::CurrentCulture) $date2 = [datetime]::Now $date1 - $date2
\_(ツ)_/
Thursday, July 20, 2017 7:02 PM
All replies
-
You can't subtract strings.
$date1 = [datetime]::ParseExact('072017 15:30:00','MMddyy HH:mm:ss',[cultureinfo]::CurrentCulture) $date2 = [datetime]::Now $date1 - $date2
\_(ツ)_/
Thursday, July 20, 2017 7:02 PM -
Hi,
Just checking in to see if the information provided was helpful. Please let us know if you would like further assistance.
Best Regards,
Frank
Please remember to mark the replies as answers if they help and unmark them if they provide no help.
If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com.Monday, July 24, 2017 9:19 AM -
Too bad the first string doesn't have slashes. Then you could do:
$Time1 = [datetime]"07/20/17 15:30:00"
$Time2 = Get-Date
$TimeDiff = $Time2 - $Time1
$TimeDiff
Monday, July 24, 2017 5:53 PM