'Get time duration from multiple datestamps Powershell
I have two sets of data that I am trying to manipulate for an expected output using PowerShell. Any ideas on how I can achieve this? Thanks for the help in advance
Data 1:
StartTime Url
2022-01-28T04:44:28.111542Z Url a
2022-02-02T13:35:15.1040997Z Url b
2022-02-02T15:33:57.961112Z Url c
Data2:
Endtime Url
2022-01-28T22:11:39.1086189Z Url m
2022-02-04T02:49:08.6644804Z Url d
Goal is table below
Startime Endtime Url of Starttime duration
2022-01-28T04:44:28.111542Z 2022-01-28T22:11:39.1086189Z Url a
2022-02-02T15:33:57.961112Z 2022-02-04T02:49:08.6644804Z Url c
Solution 1:[1]
If your input data sets are powershell object arrays
You can do:
$result = foreach ($item in $data1) {
$matchingItem = $data2 | Where-Object { $_.Url -eq $item.Url }
if ($matchingItem) {
# choose the resolution of the duration. Here I'm using TotalSeconds
# but you can also opt for TotalDays, TotalHours, TotalMinutes or TotalMilliseconds
$duration = ([datetime]$matchingItem.EndTime - [datetime]$item.StartTime).TotalSeconds
# return a new object with properties combined
[PsCustomObject]@{
StartTime = $item.StartTime
EndTime = $matchingItem.EndTime
Url = $item.Url
'Duration (Seconds)' = $duration
}
}
}
# show on screen
$result | Format-Table -AutoSize
# save as new CSV
$result | Export-Csv -Path 'X:\SomePath\durations.csv' -NoTypeInformation
Output on screen:
StartTime EndTime Url Duration (Seconds)
--------- ------- --- ------------------
2022-01-28T04:44:28.111542Z 2022-01-28T22:11:39.1086189Z Url a 62830.9970769
2022-02-02T15:33:57.961112Z 2022-02-04T02:49:08.6644804Z Url c 126910.7033684
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
