formating dates

Welcome Forums General PowerShell Q&A formating dates

This topic contains 5 replies, has 4 voices, and was last updated by

 
Participant
1 week ago.

  • Author
    Posts
  • #133430

    Participant
    Points: 180
    Rank: Participant

    I have a script that runs and queries Zabbix for data, in the return it shows the time a process is reported as having been running. I format that using regex:

    
    RunTime = $("{0:dd}:{0:hh}:{0:mm}:{0:ss}" -f $(NEW-TIMESPAN –Start $([datetime]$(((([regex]::Matches($value, '(\d+/\d+/\d\d\d\d.+): ')[0]).Value).Split(' process'))[0] + " " + ((([regex]::Matches($value, '(\d+/\d+/\d\d\d\d.+): ')[0]).Value).Split(' process'))[1] + " " +((([regex]::Matches($value, '(\d+/\d+/\d\d\d\d.+): ')[0]).Value).Split(' process'))[2])) –End $([datetime]$($output.LastClock))) )
    
    

    That returns colon separated values:

    
    01:02:27:00
    
    

    I have also tested using:

    
    {0:c}
    
    

    Which returns:

    
    1.02:27:00
    
    

    What I'm trying to achieve is:

    1 Day : 02 Hours : 27 Mins : 00 Secs

    Any suggestions on way of formatting it like that ?

     

  • #133436

    Participant
    Points: 211
    Helping Hand
    Rank: Participant

    Hi Iain – I can't simulate the same in my environment, but try the below in your code...

    
    "{0:dd} Day : {0:hh} Hours : {0:mm} Mins : {0:ss} Secs" -f ....
    
    

    Thank you.

  • #133439

    Participant
    Points: 130
    Helping Hand
    Rank: Participant
    
    $Span = New-TimeSpan -Days 1 -Hours 2 -Minutes 27 -Seconds 0
    "$($Span.Days) Day : $($Span.Hours) Hours : $($Span.Minutes) Mins : $($Span.Seconds) Secs"
    
    
  • #133455

    Participant
    Points: 384
    Helping Hand
    Rank: Contributor

    You can also call the .ToString($format) method on the DateTime object itself.

    See here for a list of custom format tokens you can use however you like:

    https://docs.microsoft.com/en-us/dotnet/standard/base-types/custom-date-and-time-format-strings

  • #133463

    Participant
    Points: 180
    Rank: Participant

    Hi Iain – I can't simulate the same in my environment, but try the below in your code...

    PowerShell
    4 lines

    1
    2
    3
    4
    "{0:dd} Day : {0:hh} Hours : {0:mm} Mins : {0:ss} Secs" -f ....
    XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

    Thank you.

    Kiran, thanks!

    I'd not been aware I could do that, seems obvious now, it gave me exactly what I was looking for

    
    01 Day 02 Hours 27 Minutes 00 Seconds
    
    

    Much appreciated.

  • #133466

    Participant
    Points: 180
    Rank: Participant
    PowerShell
    5 lines

     

    1
    2
    3
    4
    5
    $Span = New-TimeSpan Days 1 Hours 2 Minutes 27 Seconds 0
    "$($Span.Days) Day : $($Span.Hours) Hours : $($Span.Minutes) Mins : $($Span.Seconds) Secs"
    XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

    Thanks for response Sam.

You must be logged in to reply to this topic.