Substract Start Date time with End date time in Powershell.

Welcome Forums General PowerShell Q&A Substract Start Date time with End date time in Powershell.

Viewing 11 reply threads
  • Author
    Posts
    • #248299
      Participant
      Topics: 7
      Replies: 21
      Points: 111
      Rank: Participant

      Hello Experts

      I have created below code for my use, everything is working fine except the time duration object. How do I substract the End date time with Start Date time to get the job duration?

      $Jobs +=Get-DPMJob -ProtectionGroup $PG -From (Get-Date).AddDays(-$days) |? {$_.JobCategory -notlike '*Inventory*' -and $_.JobCategory -notlike 'LibraryRescan'} | Select @{l='JobName';e={$_.JobCategory}}, @{l='Status';e={$_.Status}}, @{l='ProtectionGroup';e={$_.ProtectionGroupName}}, @{l='starttime';e={$_.starttime}}, @{l='Endtime';e={$_.Endtime}}, @{l='Data Transferred(MB)';e={"{0:N2}" -f ($_.Datasize/1024/1024)}}, @{l='Duration'; e={"{0:N2}"; f={[DateTime]$_.Endtime - [DateTime]$_.Starttime}}}

      Any help would be appreciated and sorry if I couldn’t put this as a code. Not sure how to use it. Thanks.

      • This topic was modified 1 month, 2 weeks ago by grokkit.
    • #248302
      Participant
      Topics: 7
      Replies: 2458
      Points: 6,439
      Helping Hand
      Rank: Community MVP

      I cannot test but I’d think you have a syntax error in your last calulated property. Don’t you get errors?

      Try it this way:
      @{Name = 'Duration'; Expression = { "{0:N2}" -f ( [DateTime]$_.Endtime - [DateTime]$_.Starttime)}}

    • #248305
      Participant
      Topics: 7
      Replies: 21
      Points: 111
      Rank: Participant

      Hi,

      Thanks for your response. No, I don’t get any error. The Duration Column is just empty. I also tried your suggestion but it is still coming as empty.

      Thanks.

    • #248344
      Participant
      Topics: 8
      Replies: 568
      Points: 2,171
      Helping Hand
      Rank: Community Hero

      If you take the data and do it by itself, you get a formatting error. You are trying to pass a datetime object to a string formatter. I’m assuming you want seconds, so try this.

      $Jobs +=Get-DPMJob -ProtectionGroup $PG -From (Get-Date).AddDays(-$days) |? {$_.JobCategory -notlike '*Inventory*' -and $_.JobCategory -notlike 'LibraryRescan'} | Select @{l='JobName';e={$_.JobCategory}},</p><p>@{l='Status';e={$_.Status}},</p><p>@{l='ProtectionGroup';e={$_.ProtectionGroupName}},</p><p>@{l='starttime';e={$_.starttime}},</p><p>@{l='Endtime';e={$_.Endtime}},</p><p>@{l='Data Transferred(MB)';e={"{0:N2}" -f ($_.Datasize/1024/1024)}},</p><p>@{n='Duration';e={"{0:N2}" -f ([datetime]$_.Endtime - [DateTime]$_.Starttime).TotalSeconds}}

      • This reply was modified 1 month, 2 weeks ago by grokkit.
    • #248389
      Participant
      Topics: 7
      Replies: 21
      Points: 111
      Rank: Participant

      Hi Doug,

      Thanks for pitching in yet again and it worked perfectly. So the result is in seconds right? if it want the result to come in mins then do I use:  ([datetime]$_.Endtime – [DateTime]$_.Starttime).TotalMinutes}}

      Also, I noticed that you used @{n=’Duration’ instead L, could you please explain me the difference between these 2? Thank you so much again. 🙂

       

    • #248395
      Participant
      Topics: 7
      Replies: 2458
      Points: 6,439
      Helping Hand
      Rank: Community MVP

      @{n=’Duration’ instead L,

      N is short vor Name and L is short for Label … works both the same and it’s recommended to use full name. 😉

    • #248401
      Participant
      Topics: 7
      Replies: 21
      Points: 111
      Rank: Participant

      Yes I thought so but wanted to be sure. Thanks again to both of you. Is there a way I can mark your replies as answers  or thumbs up for this thread?

    • #248407
      Participant
      Topics: 8
      Replies: 568
      Points: 2,171
      Helping Hand
      Rank: Community Hero

      @Maxwell, yes if you change the .totalseconds to .totalminutes. And apologies for adding confusion, Olaf explained they are the same. I never got in the habit of L so I didn’t even think about it when typing it out. For readability for yourself and others, the full names should be used in scripts. Spacing helps too. Also in this case, you can drop the type accelerator since the value is already of that type.

      @{Name = &#039;Duration&#039;; Expression = { &quot;{0:N2}&quot; -f ($_.Endtime - $_.Starttime).TotalSeconds }}

      or

      @{Label = &#039;Duration&#039;; Expression = { &quot;{0:N2}&quot; -f ($_.Endtime - $_.Starttime).TotalSeconds }}

    • #248410
      Participant
      Topics: 8
      Replies: 568
      Points: 2,171
      Helping Hand
      Rank: Community Hero

      I really need that edit button back..

    • #248416
      Participant
      Topics: 7
      Replies: 21
      Points: 111
      Rank: Participant

      Thanks Doug. Really appreciate your help. I am still wondering if there is a way to mark your reply as ANS so that you get the much deserved points 🙂

       

    • #248425
      Participant
      Topics: 7
      Replies: 2458
      Points: 6,439
      Helping Hand
      Rank: Community MVP

      I am still wondering if there is a way to mark your reply as ANS so that you get the much deserved points

      Something like that does not exist. We are glad to be of any help for you … even without any other reward than your “Thanks” 😉

      I really need that edit button back..

      Fully agreed … :-}

    • #248443
      Participant
      Topics: 7
      Replies: 21
      Points: 111
      Rank: Participant

      Thanks a ton to both you… Stay safe. 🙂

Viewing 11 reply threads
  • You must be logged in to reply to this topic.