Jump to content

System Up Time in AM / PM


Recommended Posts

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

Link to comment
Share on other sites

  • Moderators

@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!

Link to comment
Share on other sites

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

 

Link to comment
Share on other sites

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

 

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