Sign in to follow this  
Followers 0
denvar

Returning current time in 12 hour format

26 posts in this topic

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?

Share this post


Link to post
Share on other sites

I'm not sure whether you want 24 hour format or not,

For me doing this:

#include <date.au3>
MsgBox(0,"",_Now())

Returns this:

5/24/2008 12:17:15 AM

If this were 24-hour format it would say 00:17:15 i believe

1 person likes this

Share this post


Link to post
Share on other sites

It's returning a regular 12 hour format for me as well Paulie.

Share this post


Link to post
Share on other sites

Sorry not all of my text was added to the message.

I am looking for the current time in 12 hour format, without the date

Share this post


Link to post
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

Share this post


Link to post
Share on other sites

hmm i wonder how I missed this in the helpfile when looking at _Now..

Thanks :)

Share this post


Link to post
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

Share this post


Link to post
Share on other sites

#9 ·  Posted (edited)

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

Share this post


Link to post
Share on other sites

#10 ·  Posted (edited)

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

Share this post


Link to post
Share on other sites

Or

MsgBox(0,"", _TimeHrMin())

Func _TimeHrMin($sType = 3)
    $hr = @HOUR
    If $hr > 12 Then
        $hr = $hr -12
    EndIf
    $time = $hr & ":" & @MIN
    Return ($time)
EndFuncoÝ÷ Ûú®¢×µÓ~¬O*^

8)


NEWHeader1.png

Share this post


Link to post
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

Share this post


Link to post
Share on other sites

#13 ·  Posted (edited)

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

Share this post


Link to post
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!"

Share this post


Link to post
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

Share this post


Link to post
Share on other sites

#17 ·  Posted (edited)

@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

Share this post


Link to post
Share on other sites

#18 ·  Posted (edited)

@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

Share this post


Link to post
Share on other sites

#19 ·  Posted (edited)

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

Share this post


Link to post
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!"

Share this post


Link to post
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
Sign in to follow this  
Followers 0