DigDeep Posted June 12, 2017 Posted June 12, 2017 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 JLogan3o13 Posted June 12, 2017 Moderators Posted June 12, 2017 @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!
mikell Posted June 12, 2017 Posted June 12, 2017 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)
DigDeep Posted June 12, 2017 Author Posted June 12, 2017 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
Malkey Posted June 13, 2017 Posted June 13, 2017 Another approach. expandcollapse popup#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
manpower Posted January 27 Posted January 27 I am a beginner in Autoit. i try to .... Isn't this the correct way? _NowTime() Global $H = @HOUR - 12 If $H > 0 Then $RAMPM = "PM" Else $RAMPM = "AM" EndIf
Dan_555 Posted January 27 Posted January 27 (edited) 2 hours ago, manpower said: I am a beginner in Autoit. i try to .... Isn'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 January 27 by Dan_555 manpower 1 Some of my script sourcecode
manpower Posted January 27 Posted January 27 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.
Nine Posted January 27 Posted January 27 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 “They did not know it was impossible, so they did it” ― Mark Twain Spoiler Block all input without UAC Save/Retrieve Images to/from Text Monitor Management (VCP commands) Tool to search in text (au3) files Date Range Picker Virtual Desktop Manager Sudoku Game 2020 Overlapped Named Pipe IPC HotString 2.0 - Hot keys with string x64 Bitwise Operations Multi-keyboards HotKeySet Recursive Array Display Fast and simple WCD IPC Multiple Folders Selector Printer Manager GIF Animation (cached) Debug Messages Monitor UDF Screen Scraping Round Corner GUI UDF Multi-Threading Made Easy
manpower Posted January 28 Posted January 28 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 😁
ioa747 Posted January 28 Posted January 28 ConsoleWrite(_Hour() & @CRLF) ConsoleWrite(_Hour("18") & @CRLF) Func _Hour($iHour = @HOUR) $iHour = Int($iHour) Local $PmAm = "AM" If $iHour > 12 Then $PmAm = "PM" $iHour -= 12 EndIf Return StringFormat("%02i %s", $iHour, $PmAm) EndFunc ;==>_Hour I know that I know nothing
manpower Posted January 28 Posted January 28 34 minutes ago, ioa747 said: ConsoleWrite(_Hour() & @CRLF) ConsoleWrite(_Hour("18") & @CRLF) Func _Hour($iHour = @HOUR) $iHour = Int($iHour) Local $PmAm = "AM" If $iHour > 12 Then $PmAm = "PM" $iHour -= 12 EndIf Return StringFormat("%02i %s", $iHour, $PmAm) EndFunc ;==>_Hour Oh..! thank you so much. ioa747 1
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now