-1

When you load D:\script2\IIS\timeln.txt , the time is written in one line like 09:15:05

I want to calculate this time by subtracting it from the current time

ex) 10:48:20 - 10:15:05 = 00:33:15 or 1995 seconds

I want 01:33:15 or 1995 seconds to be saved as a text file and remain

강병우
  • 1
  • 4

2 Answers2

1

You can use the New-TimeSpan function within PowerShell.

(New-TimeSpan -Start 10:15:05 -End 10:48:20).TotalSeconds
Will Walsh
  • 1,799
  • 1
  • 6
  • 15
0
# Get time string from file
$timeFromFile = Get-Content 'D:\script2\IIS\timeln.txt'

# Get timespan by substracting time form file from current date/time 
$diff = [datetime]::Now - [datetime]$timeFromFile

# Get the total seconds and round 
[System.Math]::Round($diff.TotalSeconds)

Edit

# Check if seconds > 900
if ($diff.TotalSeconds -gt 900){
    # do something
    Write-Host "Time difference is greater than 900 seconds"
    $diff.TotalSeconds | Out-File 'D:\script2\IIS\result.txt'
}
Daniel
  • 4,792
  • 2
  • 7
  • 20