Author Posts

June 26, 2013 at 5:06 am

Looking for a little help and some advise on a possible better way to do what I am trying to accomplish here. I am trying to get this script to write out the current time such as "Seven Fifty Three and Forty seconds". I can accomplish that just fine, my problem comes in when the time is between x:01 and x:09. I can't figure out how to make it say "Zero One, Zero Two" etc. or Zero seconds instead of the current "O'clock".

Can anyone give me any advice on this and possibly a way to send it the entire time such as Get-WrittenTime -objTime (Get-Date)

Function Get-WrittenTime
{
    Param
    (
        $objTime
    )
    $needsMore = $false
    Switch ($objTime)
    {
        00 {$LongTime = "O'Clock"}
        1 {$LongTime = "One"}
        2 {$LongTime = "Two"}
        3 {$LongTime = "Three"}
        4 {$LongTime = "Four"}
        5 {$LongTime = "Five"}
        6 {$LongTime = "Six"}
        7 {$LongTime = "Seven"}
        8 {$LongTime = "Eight"}
        9 {$LongTime = "Nine"}
        10 {$LongTime = "Ten"}
        11 {$LongTime = "Eleven"}
        12 {$LongTime = "Twelve"}
        13 {$LongTime = "Thirteen"}
        14 {$LongTime = "Fourteen"}
        15 {$LongTime = "Fifteen"}
        16 {$LongTime = "Sixteen"}
        17 {$LongTime = "Seventeen"}
        18 {$LongTime = "Eighteen"}
        19 {$LongTime = "Nineteen"}
        20 {$LongTime = "Twenty"}
        30 {$LongTime = "Thirty"}
        40 {$LongTime = "Forty"}
        50 {$LongTime = "Fifty"}
        default {$needsMore = $true }
    }

    if ($needsMore)
    {
        $splitTime = $objTime -split ""
        $x = 1
        foreach ($split in $splitTime)
        {
            if ($split.length -gt 0)
            {
                if ($x -eq 1)
                {
                    $z = $split + "0"
                    $fullTime += (Get-WrittenTime -objTime $z) + " "
                    $x++
                }
                Else
                {
                    $fullTime += Get-WrittenTime -objTime $split
                }
            }                
        }
        return $fullTime
    }
    return $LongTime
}

cls
$hour = Get-WrittenTime -objTime (Get-Date).Hour
$min = Get-WrittenTime -objTime (Get-Date).Minute
$sec = Get-WrittenTime -objTime (Get-Date).Second
Write-Output "$hour $min and $sec seconds"

June 26, 2013 at 9:54 am

Here are some changes that I think will get you to your goal leveraging the work you have done so far pretty easily:

1. Get the date once and store it in a variable instead of calling Get-Date 3 times.
2. After your second call to Get-WrittenTime, check if $date.Minute is less than 10. If so, prepend $min with "Zero " (e.g. $min = "Zero $min")

Then, if you want one function, rename your current Get-WrittenTime as Get-WrittenTimeInternal, and wrap the entire thing in a new Get-WrittenTime function (you can define functions inside of functions), and pass the date you get once into that function.

It isn't the most elegant solution, but without redoing what you have done already it should get you to your end goal pretty quickly.

July 1, 2013 at 3:15 am

Thanks for the help. I realize that it is not the most elegant solution but it was a quickly thrown at me challenge to see if I could get it to work.