Day format in date

Welcome Forums General PowerShell Q&A Day format in date

Viewing 3 reply threads
  • Author
    Posts
    • #258527
      Participant
      Topics: 2
      Replies: 5
      Points: 33
      Rank: Member

      $startTime = get-date
      $elapsedTime = $(get-date).AddHours(1) – $startTime
      “{0:dd}d:{0:HH}h:{0:mm}m:{0:ss}s” -f ([datetime]$elapsedTime.Ticks)

      Why does the result of this give me:

      01d:01h:00m:00s

      I would expect

      0d:01h:00m:00s

    • #258563
      Participant
      Topics: 16
      Replies: 1792
      Points: 3,290
      Helping Hand
      Rank: Community Hero

      Is there a specific reason you are not using a TimeSpan?

      Also v7.03

      • This reply was modified 3 weeks, 4 days ago by Rob Simmers.
    • #258647
      Participant
      Topics: 2
      Replies: 5
      Points: 33
      Rank: Member

      Tried this and got better results

      $startTime = get-date
      $elapsedTime = New-Timespan -End $(get-date).AddHours(1) -Start $startTime
      (“{0}d:” -f ($elapsedTime.Days) + “{0}h:” -f ($elapsedTime.Hours) + “{0}m:” -f ($elapsedTime.Minutes) + “{0}s” -f ($elapsedTime.Seconds))

    • #258692
      Participant
      Topics: 16
      Replies: 1792
      Points: 3,290
      Helping Hand
      Rank: Community Hero

      Not sure what happened, but I posted this solution last night:

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