Windows Vista Forums

Formatting TimeSpan to eliminate partial seconds

  1. #1


    Fred Guest

    Formatting TimeSpan to eliminate partial seconds

    What am I doing wrong below because no matter what I try in the way of
    "normal" formatting, I always get 23:59:59.9999 unless I forcibly remove via
    substring() the part I don't want? You'd think the straight forward approach
    would work, but for some reason, perhaps beause the object is of type
    TimeSpan, PowerShell chokes on it.

    # demonstrate PowerShell TimeSpan formatting bug
    [datetime] $today = [System.DateTime]::Today
    [datetime] $tomorrow = $today.AddDays(1).AddSeconds(-1).AddMilliseconds(-1)
    [TimeSpan] $elapsed = New-TimeSpan -Start $today -End $tomorrow
    Write-Host $elapsed
    # next line fails to produce desired output of just hours,minutes,second (no
    partial seconds)
    [string] $ts = "{0:hh:mm:ss}" -f $elapsed
    Write-Host $ts
    # next line fails to produce desired output of just hours,minutes,second (no
    partial seconds)
    [string] $kludge = [System.String]::Format("{0:hh:mm:ss}", $elapsed)
    Write-Host $kludge
    # have to forceibly trim unwanted portion of string to get desired output -
    why ?
    [string] $desired = $ts.Substring(0, 8)
    Write-Host $desired

      My System SpecsSystem Spec

  2. #2


    Robert Robelo Guest

    Re: Formatting TimeSpan to eliminate partial seconds

    Not a bug.
    There are no custom/standard format strings for TimeSpan like there are for DateTime.

    # you would have to get the desired TimeSpan values to the Format operator
    '{0:00}:{1:00}:{2:00}' -f $elapsed.Hours, $elapsed.Minutes, elapsed.Seconds
    '{0:00}:{1:00}:{2:00}' -f ($elapsed | % {$_.Hours, $_.Minutes, $_.Seconds})

    # or, like String's Substring Method, trim it with the Replace operator
    $elapsed -replace '\.\d+$'

    --
    Robert

      My System SpecsSystem Spec

Formatting TimeSpan to eliminate partial seconds

Similar Threads
Thread Forum
Eliminate Duplicates and Extract Partial Matches From List
Again requiring some help and direction. I was given a list of songs titles with their performers and asked if I could come up with something which...
General Discussion
New-TimeSpan cmdlet
How does the New-TimeSpan cmdlet know when datetime property of an object to use in a pipeline like this one? PS C:> Get-ChildItem xo |...
PowerShell
Timespan SMTP Received headers
This reads a set of SMTP message headers, and dislays the MTAs it passed through in chronological order, along with a calculated latency at each...
PowerShell
Is it possible to eliminate redundancy?
I would like to know if it is possible to eliminate (or turn off) the redundant "are you sure you want to move this file to the recycle bin" box?...
Vista General
Decode Unusual Timespan
I have a 'timespan' which is specified in an unusual way. For example, to specify 47 hours and 26 minutes, the timepsan is specified as 47:26 ...
.NET General
Compare timespan of Files?
II want to compare how many seconds there are between files. If the files are within a 1 - 10 second range I want to copy them to their own folders....
.NET General
How do I eliminate the welcome screen?
I seem to have seen an answer to this before, but can't find it now. The first few times I logged on ti=o Vista 5534, I went directly to the...
Vista account administration