Jump to content

Returning current time in 12 hour format


Recommended Posts

Also to keep in tune with standards midnight ($hr = 0) should be shown as 12 am instead of 00 am

Func _TimeHrMin()
    $hr = @HOUR
    If $hr <> 12 Then
        If $hr > 12 Then
            $hr = $hr -12
            $x = "PM"
        ElseIf $hr = 0 Then
            $hr = 12
            $x = "AM"
        Else
            $x = "AM"
        EndIf
    Else
        $x = "PM"
    EndIf
    $time = $hr & ":" & @MIN & " " & $x
    Return ($time)
EndFunc
Ah nice fix :)

[font="Georgia"]Chances are, I'm wrong.[/font]HotKey trouble?Stringregexp GuideAutoIT Current Version

Link to comment
Share on other sites

@Valuater

One other thing. Reading your reply reminded me that I've wanted to re-write that function for a long time and I kept forgetting to do it. Old age defense again. I can quickly whip it up to allow for international differences. However it willl make the function longer by a few lines.

Example; We use AM/PM notation for time, but that's not true in all locales. I already have that part done done. Now I'll start on the separator character (:)

Of course for those areas where they don't use the 12 time format, they don't need this function at all.

George

Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.

Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.***

The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number.

Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else.

"Old age and treachery will always overcome youth and skill!"

Link to comment
Share on other sites

@Val

Funny thing, I have that exact code written in one of my funcs and I suspect others do as well. The only diff that I see is I have a leading space in $ampm for readability.

Most likely, Thats because it has been in Autoit Wrappers for a long time now

@Val

I have just noticed 12 is not handled correctly, for instance 12:05 in the afternoon is being shown as am not pm

As noted above I didn't just write that, just make the minor change as needed :)

8)

Edited by Valuater

NEWHeader1.png

Link to comment
Share on other sites

Here is the modified function that will allow for the locale and seconds if desired. It uses the values from the registry

Func Time($iSec = 0)
  Local $fKey = "HKCU\Control Panel\International", $aStr = RegRead($fKey, "s1159")
  Local $pStr = RegRead($fKey, "s2359"), $tSep = RegRead($fKey, "sTime"), $sStr = ""
  Local $ap = ""
  $hour = @Hour
  Select
    Case $hour <= 11
      $ap = $aStr
      If $hour = 0 Then $hour = 12
    Case Else
      If $hour > 12 Then $hour -= 12
      $ap = $pStr
  EndSelect
  If $iSec <> 0 Then $sStr = $tSep & @Sec
  Return $hour & $tSep & @Min & $sStr & Chr(32) & $ap
EndFunc  ;<==> Time()

Call it like so

MsgBox(0, "TEST", Time())

Calling it with any numeric parameter except 0 will cause the seconds to also be returned.

Example

MsgBox(0, "TEST", Time(1))

To remove the space before AM/PM just remove the & Chr(32) from the return line

Enjoy

George

Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.

Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.***

The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number.

Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else.

"Old age and treachery will always overcome youth and skill!"

Link to comment
Share on other sites

  • 5 years later...

12 Hour Clock Format

I'm just try...

#include <GuiConstants.au3>

GuiCreate('CLOCK', 350, 100)
$Time = GUICtrlCreateLabel('', 5, 5, 340, 90, BitOr(0x01,0x0200))
GUICtrlSetFont(-1, 50, 600, 0, 'Arial Narrow')
GUICtrlSetColor(-1, 0xAF0808)
GuiSetState()

While 1
    $Msg = GUIGetMsg()
    Select
        Case $Msg = $GUI_EVENT_CLOSE
        ExitLoop
    EndSelect
    _12_Hour_Clock()
WEnd

Func _12_Hour_Clock()
    $AM_PM = 'AM'
    $HOUR = @HOUR
    If $HOUR > 11 Then $AM_PM = 'PM'
    Switch $HOUR
        Case 0
            $HOUR = 12
        Case 1 To 9
            $HOUR = StringRight($HOUR, 1)
        Case 10 To 12
        Case 13 To 23
            $HOUR-= 12
        Case Else
            $HOUR = ''
    EndSwitch

    $12_Hour_Clock = $HOUR&':'&@MIN&':'&@SEC&' '&$AM_PM

    Global $Time_Now
    If $Time_Now <> $12_Hour_Clock Then
        $Time_Now = $12_Hour_Clock
        GUICtrlSetData ($Time, $Time_Now)
    EndIf
EndFunc
Link to comment
Share on other sites

Do you think it is sensible to post in a 5 1/2 years old thread?

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2022-02-19 - Version 1.6.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...