Public/Format-PfaSecondsFromMidnight.ps1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
function Format-PfaSecondsFromMidnight {
    <#
    .SYNOPSIS
    Formats seconds from midnight into a user friendly time.
     
    .DESCRIPTION
    Formats seconds from midnight into a user friendly time.
     
    .PARAMETER Seconds
    The number of seconds to convert.
 
    .PARAMETER AsInt
    Return value is an integer only.
 
    .EXAMPLE
    Format-PfaSecondsFromMidnight -Seconds 14400
    4am
 
    .EXAMPLE
    Format-PfaSecondsFromMidnight -Seconds 14400 -AsInt
    4
 
    .NOTES
    Author: brandon said
    #>

    [CmdletBinding()]
    param (
        [Parameter(Mandatory = $true, Position = 0)][AllowNull()]
        $Seconds,
        [Parameter(Mandatory = $false)]
        [Switch]$AsInt
    )
    if ($null -ne $Seconds) {
        if (-not $AsInt) {
            (Get-Date).Date.AddSeconds([Int64]$Seconds).ToString("htt").ToLower()
        } else {
            [Int64](Get-Date).Date.AddSeconds([Int64]$Seconds).ToString("h ").Trim()
        }
    } else {
        if (-not $AsInt) {
            "-"
        } else {
            [Int64]0
        }
    }
}