Jump to content

Recommended Posts

Posted

Can someone help me to get this in 12 hour format and AM / PM

 

#include <Date.au3>
#include <Process.au3>


UpTime()

Func UpTime()
    Local $pclogs = 'C:\Temp'
    $result = $pclogs & '\Uptime.log'
    _RunDos(@SystemDir & '\wbem\WMIC.exe OS get LastBootUpTime > ' & $result)
        $GetDate = StringLeft(FileReadLine($result, 2), 14)
    $BootDate = StringMid($GetDate, 5, 2) & '/' & StringRight($GetDate, 2) & '/' & StringLeft($GetDate, 4) & " - " & StringMid($GetDate, 9, 2)& ":" & StringMid($GetDate, 11, 2)

MsgBox(0, '', $BootDate)
EndFunc

I am getting the result as:

6/13/2017 - 22:50

I would need this as:

6/13/2017 - 8:50 PM

  • Moderators
Posted

@DigDeep You've been around long enough to know the difference between posting a question in Help and Support, and posting in the Examples forum. Please be mindful of where you create topics.

"Profanity is the last vestige of the feeble mind. For the man who cannot express himself forcibly through intellect must do so through shock and awe" - Spencer W. Kimball

How to get your question answered on this forum!

Posted

thanks @mikell

During this time I was trying almost the same.

UpTime()

Func UpTime()
    Local $pclogs = 'C:\Temp'
    $result = $pclogs & '\Uptime.log'
    _RunDos(@SystemDir & '\wbem\WMIC.exe OS get LastBootUpTime > ' & $result)
        $GetDate = StringLeft(FileReadLine($result, 2), 14)

        $GetHour = (StringMid($GetDate, 9, 2) - 12)
        $GetHourMode = StringMid($GetDate, 9, 2)
        If $GetHourMode >= 12 Then
            $BootDate = StringMid($GetDate, 5, 2) & '/' & StringRight($GetDate, 2) & '/' & StringLeft($GetDate, 4) & " - " & $GetHour & ":" & StringMid($GetDate, 11, 2) & " PM."
        Else
            $BootDate = StringMid($GetDate, 5, 2) & '/' & StringRight($GetDate, 2) & '/' & StringLeft($GetDate, 4) & " - " & $GetHour & ":" & StringMid($GetDate, 11, 2) & " AM."
            EndIf

MsgBox(0, '', $BootDate)
EndFunc

 

Posted

Another approach.

#include <Date.au3>

MsgBox(0, 'Results', _DateFormat(UpTime(), "dddd, MM/dd/yyyy h:mm tt."))


; Result from Dos command is re-formated to "yyyy/MM/dd HH:mm:ss" as a parameter for the _DateFormat() function.
Func UpTime()
    Local $Cmd = _Cmd("C:\Windows\System32\wbem\WMIC.exe OS get LastBootUpTime")
    Return StringRegExpReplace($Cmd, "(\d{4})(\d{2})(\d{2})(\d{2})(\d{2})(\d{2}).+", "\1/\2/\3 \4:\5:\6") ; Uses first 14 digits from left.
EndFunc   ;==>UpTime

; Run a Dos command - $sCmd.
Func _Cmd($sCmd, $sDir = "")
    Local $text = '', $Pid = Run(@ComSpec & " /c " & $sCmd, $sDir, @SW_HIDE, 2 + 4)
    While 1
        $text &= StdoutRead($Pid, False, False)
        If @error Then ExitLoop
        Sleep(10)
    WEnd
    While 1
        $text &= StderrRead($Pid)
        If @error Then ExitLoop
        ConsoleWrite(@LF & "!#------------------------# Error #---------------------------# " & @LF)
    WEnd
    Return $text
EndFunc   ;==>_Cmd

; -------------- _DateFormat([$DateTime ][, $sFormat]) --------------
;Parameters:-
;    $DateTime - Dates "yyyy/MM/dd", "yyyy-MM-dd", or "yyyy.MM.dd", with or without, Time "HH:mm;ss".
;    $sFormat  - A string using custom date and time format characters. See reference:-
; https://docs.microsoft.com/en-us/dotnet/standard/base-types/custom-date-and-time-format-strings#the-hh-custom-format-specifier-1
; Returns: Date/Time in the format as stated in $sFormat.
;
Func _DateFormat($DateTime = "", $sFormat = "dd/MM/yyyy HH:mm:ss")
    If $DateTime == "" Then $DateTime = _NowCalc()
    If StringRegExp($DateTime, "\d{4}([/\-\.])\d{1,2}\g{-1}\d{1,2}") = 0 Then $DateTime = _NowCalcDate() & " " & $DateTime
    GUICreate("")
    GUICtrlCreateDate($DateTime, 0, 0)
    GUICtrlSendMsg(-1, 0x1032, 0, $sFormat)
    Return GUICtrlRead(-1)
EndFunc   ;==>_DateFormat

 

  • 7 years later...
Posted (edited)
2 hours ago, manpower said:

I am a beginner in Autoit.

i try to ....

download.gif.618802cd8ef749f2473f53cd4fe1e78a.gifdownload.gif.69ce4694383074c0d439f66397227f46.gifIsn't this the correct way?

_NowTime()
Global $H = @HOUR - 12
If $H > 0 Then 
    $RAMPM = "PM"
Else
    $RAMPM = "AM"
EndIf

 

If the @hour returns following:

0   1  2  3  4  5  6  7  8  9 10 11
12 13 14 15 16 17 18 19 20 21 22 23

then, probably, you should use =>0

 

Edited by Dan_555

Some of my script sourcecode

Posted
1 hour ago, Dan_555 said:

If the @hour returns following:

0   1  2  3  4  5  6  7  8  9 10 11
12 13 14 15 16 17 18 19 20 21 22 23

then, probably, you should use =>0

 

Thanks you.

i'm more study.

Posted

Strictly speaking, @HOUR returns a string.  As per help file :

Quote
@HOUR     Hours value of clock in 24-hour format. Range is 00 to 23

Although it would work mixing string and number, it is a good practice to convert the string into a number before comparing it to another number.

Global $H = Number(@HOUR) - 12

You do not need to use an intermediate variable ($H), you could simply use this :

If Number(@HOUR) >= 12 Then

 

Posted
12 hours ago, Nine said:

Strictly speaking, @HOUR returns a string.  As per help file :

Although it would work mixing string and number, it is a good practice to convert the string into a number before comparing it to another number.

Global $H = Number(@HOUR) - 12

You do not need to use an intermediate variable ($H), you could simply use this :

If Number(@HOUR) >= 12 Then

 

Thanks for letting me know.

Sorry for my poor English.

.... display AM is Good  

Pre  -4:00 AM  After 4:00 AM   😁

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
  • Recently Browsing   0 members

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