Jump to content

Returning current time in 12 hour format


Recommended Posts

I have seen and tried functions like _Now() and _Date_Time_GetLocalTime() and _Date_Time_SystemTimeToArray() however these return the time in 24 hour format. (So 4:05pm instead of 16:05)

Any ideas?

Link to comment
Share on other sites

#include<Date.au3>

ConsoleWrite(_NowTime() & @CRLF)

Scripts & functions Organize Includes Let Scite organize the include files

Yahtzee The game "Yahtzee" (Kniffel, DiceLion)

LoginWrapper Secure scripts by adding a query (authentication)

_RunOnlyOnThis UDF Make sure that a script can only be executed on ... (Windows / HD / ...)

Internet-Café Server/Client Application Open CD, Start Browser, Lock remote client, etc.

MultipleFuncsWithOneHotkey Start different funcs by hitting one hotkey different times

Link to comment
Share on other sites

Is it possible using _NowTime to return only time in hour and minutes?

The helpfile seems to be referring to dates not times so is confusing, can only see using $sType = 4 however that displays 24 hour format

Am trying to achieve : 5:15 pm for example

Link to comment
Share on other sites

Or

MsgBox(0,"", _TimeHrMin())

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

EDIT: Added the AM and PM :)

Edited by aslani

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

Link to comment
Share on other sites

How about:

MsgBox(0, "", _AmPmTimeIs())

Func _AmPmTimeIs()
    $ampm = "am"
    $Hour = @HOUR
    If $Hour > 12 Then
        $Hour -= 12
        $ampm = "pm"
    EndIf
    $Min = @MIN
    $time = $Hour & ":" & $Min & " " & $ampm
    Return $time
EndFunc

Edit: aslani sorta beat me to it, but mine adds the am/pm indicator :)

Edit 2:hey aslani, we gotta stop posting/editing at the same time :)

Edited by ResNullius
Link to comment
Share on other sites

Thanks for the input guys.

Using Valuater's code and it seems to be working a charm, was hoping would have been a simpler way to do this, woyld have though h:mm am/pm would be more commonly required then h:mm:ss

Anyway alls well that ends well, thanks guys

Link to comment
Share on other sites

Is this right?

CODE
Select

Case @HOUR = 0 And @SEC=0 And @MIN=0

$hour = 12

$AMPM = "a.m."

Case @HOUR = 12 And @SEC=0 And @MIN=0

$hour = 12

$AMPM = "p.m."

Case @HOUR > 11

$hour = @HOUR - 12

$AMPM = "p.m."

Case Else

$hour = @HOUR

$AMPM = "a.m."

EndSelect

$time12 = StringFormat("%02d:%02d:%02d %s",$hour,@MIN,@SEC,$AMPM)

MsgBox(0, '', $time12)

Btw: 12 noon = 12:00 PM

....... 12 midnight = 12:00 AM

//Edit: Oh, no forgot to reload

Edited by ProgAndy

*GERMAN* [note: you are not allowed to remove author / modified info from my UDFs]My UDFs:[_SetImageBinaryToCtrl] [_TaskDialog] [AutoItObject] [Animated GIF (GDI+)] [ClipPut for Image] [FreeImage] [GDI32 UDFs] [GDIPlus Progressbar] [Hotkey-Selector] [Multiline Inputbox] [MySQL without ODBC] [RichEdit UDFs] [SpeechAPI Example] [WinHTTP]UDFs included in AutoIt: FTP_Ex (as FTPEx), _WinAPI_SetLayeredWindowAttributes

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.

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

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

Try this

MsgBox(0,"", _TimeHrMin())

Func _TimeHrMin()
    $hr = @HOUR
    If $hr <> 12 Then
        If $hr > 12 Then
            $hr = $hr -12
            $x = "PM"
        Else
            $x = "AM"
        EndIf
    Else
        $x = "PM"
    EndIf
    $time = $hr & ":" & @MIN & " " & $x
    Return ($time)
EndFunc
Edited by aslani

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

Link to comment
Share on other sites

@Aslani

Noon would lead to x not being declared

This is solved with

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

//Edit : beat me to it :)

Edited by denvar
Link to comment
Share on other sites

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
Edited by denvar
Link to comment
Share on other sites

@ProgAndy

You don't need the last part of the lines containing

And @SEC=0 And @MIN=0

All you need is

Case @Hour =

@Sec and @Min are being handled later anyway.

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

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