DigDeep

System Up Time in AM / PM

5 posts in this topic

#1 ·  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

Share this post


Link to post
Share on other sites



#2 ·  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.


√-1 2^3 ∑ π, and it was delicious!

Share this post


Link to post
Share on other sites

#3 ·  Posted

Did you try something ? so easy...

Local $ampm, $h = @HOUR, $m = @MIN
If $h<12 Then
  $ampm = "AM"
Else
  $ampm = "PM"
  $h -= 12
EndIf
Msgbox(0,"", $h & ":" & $m & " " & $ampm)

 

Share this post


Link to post
Share on other sites

#4 ·  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

 

Share this post


Link to post
Share on other sites

#5 ·  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

 

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