formating dates

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 ?

 

Hi Iain - I can’t simulate the same in my environment, but try the below in your code…

[pre]

“{0:dd} Day : {0:hh} Hours : {0:mm} Mins : {0:ss} Secs” -f …

[/pre]

Thank you.

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

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

[quote quote=133436]Hi Iain – I can’t simulate the same in my environment, but try the below in your code…

PowerShell
4 lines
<textarea class="ace_text-input" style="opacity: 0; height: 18px; width: 7px; left: 44px; top: 0px;" spellcheck="false" wrap="off"></textarea>
1
2
3
4
"{0:dd} Day : {0:hh} Hours : {0:mm} Mins : {0:ss} Secs" -f ....
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
Thank you.

[/quote]

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.

Thanks for response Sam.