Day format in date

$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

Powershell Core 7.0.3 does same thing

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

PS C:\Users\rasim> 
$startTime = get-date
$elapsedTime = $(get-date).AddHours(1)

#https://docs.microsoft.com/en-us/dotnet/standard/base-types/standard-timespan-format-strings
'Standard (c) - {0:c}' -f (New-TimeSpan -Start $startTime -End $elapsedTime)
'Standard (g) - {0:g}' -f (New-TimeSpan -Start $startTime -End $elapsedTime)
'Standard (G) - {0:G}' -f (New-TimeSpan -Start $startTime -End $elapsedTime)
#https://docs.microsoft.com/en-us/dotnet/standard/base-types/custom-timespan-format-strings
“Custom - {0:dd}d:{0:hh}h:{0:mm}m:{0:ss}s” -f (New-TimeSpan -Start $startTime -End $elapsedTime)

Standard (c) - 01:00:00
Standard (g) - 1:00:00
Standard (G) - 0:01:00:00.0000000
Custom - 00d:01h:00m:00s

PS C:\Users\rasim> $PSVersionTable

Name                           Value                                                                                                                                                                                                     
----                           -----                                                                                                                                                                                                     
PSVersion                      5.1.19041.1                                                                                                                                                                                               
PSEdition                      Desktop                                                                                                                                                                                                   
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0...}                                                                                                                                                                                   
BuildVersion                   10.0.19041.1                                                                                                                                                                                              
CLRVersion                     4.0.30319.42000                                                                                                                                                                                           
WSManStackVersion              3.0                                                                                                                                                                                                       
PSRemotingProtocolVersion      2.3                                                                                                                                                                                                       
SerializationVersion           1.1.0.1  

Also v7.03

PS C:\Users\rasim> 
$startTime = get-date
$elapsedTime = $(get-date).AddHours(1)

#https://docs.microsoft.com/en-us/dotnet/standard/base-types/standard-timespan-format-strings
'Standard (c) - {0:c}' -f (New-TimeSpan -Start $startTime -End $elapsedTime)
'Standard (g) - {0:g}' -f (New-TimeSpan -Start $startTime -End $elapsedTime)
'Standard (G) - {0:G}' -f (New-TimeSpan -Start $startTime -End $elapsedTime)
#https://docs.microsoft.com/en-us/dotnet/standard/base-types/custom-timespan-format-strings
“Custom - {0:dd}d:{0:hh}h:{0:mm}m:{0:ss}s” -f (New-TimeSpan -Start $startTime -End $elapsedTime)

Standard (c) - 01:00:00.0013600
Standard (g) - 1:00:00.00136     
Standard (G) - 0:01:00:00.0013600
Custom - 00d:01h:00m:00s

PS C:\Users\rasim> $psversiontable


Name                           Value
----                           -----
PSVersion                      7.0.3
PSEdition                      Core
GitCommitId                    7.0.3
OS                             Microsoft Windows 10.0.19041
Platform                       Win32NT
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0…}
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1
WSManStackVersion              3.0

[datetime]$elapsedTime.Ticks equates to ‘01 January 0001 01:00:00’, so the day part is 1.

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))

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

#https://docs.microsoft.com/en-us/dotnet/standard/base-types/standard-timespan-format-strings
'Standard (c) - {0:c}' -f (New-TimeSpan -Start $startTime -End $elapsedTime)
'Standard (g) - {0:g}' -f (New-TimeSpan -Start $startTime -End $elapsedTime)
'Standard (G) - {0:G}' -f (New-TimeSpan -Start $startTime -End $elapsedTime)
#https://docs.microsoft.com/en-us/dotnet/standard/base-types/custom-timespan-format-strings
“Custom - {0:dd}d:{0:hh}h:{0:mm}m:{0:ss}s” -f (New-TimeSpan -Start $startTime -End $elapsedTime)

Standard (c) - 01:00:00
Standard (g) - 1:00:00
Standard (G) - 0:01:00:00.0000000
Custom - 00d:01h:00m:00s