# formating dates

**URL:** <https://forums.powershell.org/t/formating-dates/11866>\
**Category:** PowerShell Help\
**Created:** [January 8, 2019, 9:16am UTC](https://forums.powershell.org/t/formating-dates/11866 "2019-01-08T09:16:30Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![firmbyte](https://avatars.discourse-cdn.com/v4/letter/f/e68b1a/32.png) [@firmbyte](https://forums.powershell.org/u/firmbyte)\
**Post date:** [January 8, 2019, 9:16am UTC](https://forums.powershell.org/t/formating-dates/11866/1 "2019-01-08T09:16:30Z")

</div>

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 ?

&nbsp;

---

<div class="post-metadata">

**Author:** ![kpatnayakuni](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/kpatnayakuni/32/19_2.png) [@kpatnayakuni](https://forums.powershell.org/u/kpatnayakuni)\
**Post date:** [January 8, 2019, 9:43am UTC](https://forums.powershell.org/t/formating-dates/11866/2 "2019-01-08T09:43:49Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![sam-boutros](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/sam-boutros/32/5_2.png) [@sam-boutros](https://forums.powershell.org/u/sam-boutros)\
**Post date:** [January 8, 2019, 9:45am UTC](https://forums.powershell.org/t/formating-dates/11866/3 "2019-01-08T09:45:07Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![ta11ow](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/ta11ow/32/131_2.png) [@ta11ow](https://forums.powershell.org/u/ta11ow)\
**Post date:** [January 8, 2019, 10:43am UTC](https://forums.powershell.org/t/formating-dates/11866/4 "2019-01-08T10:43:51Z")

</div>

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:

> **[Custom date and time format strings](https://docs.microsoft.com/en-us/dotnet/standard/base-types/custom-date-and-time-format-strings)**
>
> Learn to use custom date and time format strings to convert DateTime or DateTimeOffset values into text representations, or to parse strings for dates & times.

---

<div class="post-metadata">

**Author:** ![firmbyte](https://avatars.discourse-cdn.com/v4/letter/f/e68b1a/32.png) [@firmbyte](https://forums.powershell.org/u/firmbyte)\
**Post date:** [January 8, 2019, 11:43am UTC](https://forums.powershell.org/t/formating-dates/11866/5 "2019-01-08T11:43:01Z")

</div>

[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.

---

<div class="post-metadata">

**Author:** ![firmbyte](https://avatars.discourse-cdn.com/v4/letter/f/e68b1a/32.png) [@firmbyte](https://forums.powershell.org/u/firmbyte)\
**Post date:** [January 8, 2019, 11:44am UTC](https://forums.powershell.org/t/formating-dates/11866/6 "2019-01-08T11:44:54Z")

</div>

> [@](#):
>
> PowerShell
> 
> 5 lines
> 
> &nbsp;
> 
> 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.

---

<div class="post-metadata">

**Author:** ![dotnVo](https://avatars.discourse-cdn.com/v4/letter/d/4af34b/32.png) [@dotnVo](https://forums.powershell.org/u/dotnVo)\
**Post date:** [May 16, 2024, 8:33pm UTC](https://forums.powershell.org/t/formating-dates/11866/7 "2024-05-16T20:33:28Z")

</div>

Closed on May 16, 2024, 8:33pm UTC


