PowerShell
PowerShell

Reputation: 2081

Subtract values in an array

i have the below data in a variable, how do i subtract the time between all the entries ?, right now there are only 3 entries, there can be 1,2...100 or more, how can i create a single variable which has the value of Time[0]-Time[1]...Time[n] as they $count is dynamic ?

PS /Users/> $Count

    Time         : 3/31/2020 9:47:19 PM
    DateFormat   : 31/03/2020
    Username     : ONE\a1awp7hpd


    Time         : 3/31/2020 1:00:43 PM
    DateFormat   : 31/03/2020
    Username     : ONE\a1wp7hpd

    Time         : 3/31/2020 12:25:31 PM
    DateFormat   : 31/03/2020
    Username     : ONE\a1wp7hpd

Upvotes: 0

Views: 246

Answers (1)

Mathias R. Jessen
Mathias R. Jessen

Reputation: 174690

As mentioned in the comments, summation of the difference between each timestamp is exactly the same as the difference between the first and last timestamp, so let's do that:

  • Sort the list by the timestamp (if they aren't already sorted)
  • Calculate [newest] - [oldest]:
$oldest,$newest = @($Count |Select -Expand Time |Sort-Object)[-1,0]

$difference = $newest - $oldest

Upvotes: 2

Related Questions