Snippets ( Time & Date )

From AutoIt Wiki
Jump to navigation Jump to search

Please always credit an author in your script if you use their code. It is only polite.


30 Day Trial

Author: MSLx Fanboy








; 30 Day Trial #include <Date.au3> #include <String.au3> If RegRead("HKCU\Software\Microsoft\Windows\Current Version", "XPClean Menu") = "" Then RegWrite("HKCU\Software\Microsoft\Windows\Current Version", "XPClean Menu", "REG_SZ", _StringEncrypt(1, _NowCalc(), @ComputerName)) SetError(0) EndIf $startdate = _StringEncrypt(0, RegRead("HKCU\Software\Microsoft\Windows\Current Version", "XPClean Menu"), @ComputerName) If _DateDiff("D", $startdate, _NowCalc()) > 30 Then MsgBox(0, "*XPClean Menu*", "Your registration period has expired.") Exit EndIf 

Return To Contents

_ChangeTime

Author: guinness








_ChangeTime(@HOUR, @MIN) Func _ChangeTime($iHour, $iMin) Local $sTime = $iHour & RegRead("HKEY_CURRENT_USER\Control Panel\International", "sTime") & $iMin RunWait(@ComSpec & " /c Time " & $sTime, "", @SW_HIDE) Return $sTime EndFunc ;==>_ChangeTime 

Return To Contents

_ChangeDate

Author: guinness








#include <Date.au3> _ChangeDate(@YEAR, @MON , @MDAY) Func _ChangeDate($iYear, $iMon, $iDay) Local $sDate = _DateTimeFormat($iDay & "/" & $iMon & "/" & $iYear, 2) RunWait(@ComSpec & " /c Date " & $sDate, "", @SW_HIDE) Return $sDate EndFunc ;==>_ChangeDate 

Return To Contents

Check File Date To Current Date

Author: Jos








; Check File Date To Current Date #include<date.au3> $n_tFile = "your file name" $n_Fdate = FileGetTime($n_tFile,1) $sdate=$n_Fdate[0] & "/" & $n_Fdate[1] & "/" & $n_Fdate[2] & " " & $n_Fdate[3] & ":" & $n_Fdate[4] & ":" & $n_Fdate[5] $edate=_NowCalc() If _DateDiff ('d',$sdate,$eDate) > 5 then ; EndIf 

Return To Contents

_CheckIdle

Author: GaryFrost








; User/System Idle Time #include <Date.au3> HotKeySet("{Esc}", "_Terminate") Local $last_active = 0, $iHours, $iMins, $iSecs Local $not_idle = _CheckIdle($last_active, 1) while (1) Sleep(200) $not_idle = _CheckIdle($last_active) _TicksToTime($not_idle, $iHours, $iMins, $iSecs) If $iHours Or $iMins Or $iSecs Then ConsoleWrite("Was Idle for: Hours: " & $iHours & " Minutes: " & $iMins & " Seconds: " & $iSecs & @LF) EndIf WEnd Func _CheckIdle(ByRef $last_active, $start = 0) Local $struct = DllStructCreate("uint;dword"); DllStructSetData($struct, 1, DllStructGetSize($struct)); If $start Then DllCall("user32.dll", "int", "GetLastInputInfo", "ptr", DllStructGetPtr($struct)) $last_active = DllStructGetData($struct, 2) Return $last_active Else DllCall("user32.dll", "int", "GetLastInputInfo", "ptr", DllStructGetPtr($struct)) If $last_active <> DllStructGetData($struct, 2) Then Local $save = $last_active $last_active = DllStructGetData($struct, 2) Return $last_active - $save EndIf EndIf EndFunc ;==>_CheckIdle() Func _Terminate() Exit EndFunc ;==>_Terminate 

Return To Contents

_CopyrightYear

Author: guinness








ConsoleWrite(Chr(169) & " " & _CopyrightYear("2010") & @LF) ; Pass a String of 2010. This is the year the software was created. ConsoleWrite(_CopyrightYear(2010) & @LF) ; Pass a Number of 2010. ConsoleWrite(_CopyrightYear(2012) & @LF) ; Pass a Number of 2012. Func _CopyrightYear($iStartYear, $sDelimeter = "-") ; Return a String representation. If Number($iStartYear) <> @YEAR Then Return String($iStartYear & " " & $sDelimeter & " " & @YEAR) EndIf Return String($iStartYear) EndFunc ;==>_CopyrightYear 

Return To Contents

_GetQuarterlyValue

Author: guinness








ConsoleWrite('The current quarterly value is: ' & _GetQuarterlyValue() & @CRLF) ; Get the current quarter of the year (1–4). Func _GetQuarterlyValue() Local $oWMIService = ObjGet('winmgmts:\\.\root\CIMV2'), $sReturn = '' Local $oColItems = $oWMIService.ExecQuery('Select Quarter From Win32_UTCTime', 'WQL') If IsObj($oColItems) Then For $oItem In $oColItems Return $oItem.Quarter Next EndIf EndFunc ;==>_GetQuarterlyValue 

Return To Contents

Run A Timed Program Daily

Author: Valuater








; Run a timed program daily ; #NoTrayIcon ; for testing Global $Minutes = 30 Global $Title = "My Window Title" ; settings Global $Show_Clock = 1 ; 0 = no show Global $Clock_Title = $Minutes & " Minute Time Machine" If WinExists($Clock_Title) Then Exit AutoItWinSetTitle($Clock_Title) ; ***** for testing only ****** HotKeySet("{F9}", "Runner") Func Runner() Run("notepad.exe") EndFunc ;==>Runner $Minutes = 3 ; for testing $Title = "Untitled" ; for testing ; ***************************** While 1 If WinExists($Title) Then Clockit() Sleep(100) WEnd Func Clockit() Local $log = @WindowsDir & "\temp\" Local $log_file = $log & @YDAY & ".pak" If Not FileExists($log_file) Then FileDelete($log & "*.pak") FileWriteLine($log_file, $Minutes) EndIf Local $M_Minutes = FileReadLine($log_file, 1) Local $begin = TimerInit(), $60Count = 0 If $M_Minutes <= 0 Then WinClose($Title) MsgBox(64, $Clock_Title, "Time-Up!! ...Your daily time usage has passed. ", 5) Return EndIf While $M_Minutes > 0 And WinExists($Title) $dif = TimerDiff($begin) $Count = Int($dif / 1000) If $Count >= 60 Then $60Count += 1 $M_Minutes -= 1 $begin = TimerInit() EndIf If $Show_Clock Then ToolTip("Minutes Remaining = " & $M_Minutes & @CRLF & "Minutes Past = " & $60Count & @CRLF & "Seconds Count = " & $Count, 20, 20, $Clock_Title, 1) Sleep(100) WEnd ToolTip("") FileDelete($log_file) Sleep(300) If $Count >= 20 And $M_Minutes > 0 Then $M_Minutes -= 1 FileWriteLine($log_file, $M_Minutes) EndFunc ;==>Clockit 

Return To Contents

_Time

Author: GEOSoft








ConsoleWrite("The Time Is " & _Time() & @CRLF) Func _Time($iSec = 0, $tFormat = 12) Local $fKey = "HKCU\Control Panel\International", $ap = RegRead($fKey, "s1159") Local $pStr = RegRead($fKey, "s2359"), $tSep = RegRead($fKey, "sTime"), $sStr = "" Local $hour = @HOUR If $tFormat = 12 Then If $hour = 0 Then $hour = 12 If @HOUR >= 12 Then $hour = @HOUR If $hour > 12 Then $hour -= 12 $ap = $pStr EndIf Else $ap = "" $hour = StringFormat("%02u", $hour) EndIf If $iSec <> 0 Then $sStr = $tSep & @SEC If $ap <> "" Then $ap = Chr(32) & $ap Return $hour & $tSep & @MIN & $sStr & $ap EndFunc ;==>_Time 

Return To Contents

_Time

Author: SmOke_N








ConsoleWrite(_Time() & @CRLF) Func _Time() Local $AMPM, $hour If @HOUR > 12 Then $hour = @HOUR - 12 $AMPM = "PM" ElseIf @HOUR = 0 Then $hour = 12 $AMPM = "AM" Else $hour = @HOUR $AMPM = "AM" EndIf Return $hour & ":" & @MIN & $AMPM EndFunc ;==>_Time 

Return To Contents

_Time

Author: Valuater








MsgBox(0, "What is the time?", _Time()) Func _Time() Local $hour = @HOUR, $AMPM = "AM" If $hour > 11 Then $AMPM = "PM" If $hour = 0 Then $hour = 12 If $hour > 12 Then $hour -= 12 Return $hour & ":" & @MIN & " " & $AMPM EndFunc ;==>_Time 

Return To Contents

Timer To "Thousandths" Of A Second

Author: Holger








; Timer To "Thousandths" Of A Second While 1 ToolTip(@Hour & ':' & @Min & ':' & @Sec & ':' & _MSec()) Sleep(1) WEnd Exit Func _MSec() Local $stSystemTime = DllStructCreate('ushort;ushort;ushort;ushort;ushort;ushort;ushort;ushort') DllCall('kernel32.dll', 'none', 'GetSystemTime', 'ptr', DllStructGetPtr($stSystemTime)) Local $sMilliSeconds = StringFormat('%03d', DllStructGetData($stSystemTime, 8)) $stSystemTime = 0 Return $sMilliSeconds EndFunc 

Return To Contents

_TimeFormatLocal

Author: GEOSoft








; Return a time string using the local settings format. MsgBox(4096, "", _TimeFormatLocal(@HOUR & @MIN & @SEC)) MsgBox(4096, "", _TimeFormatLocal(@HOUR & "h" & @MIN & ":" & @SEC)) Func _TimeFormatLocal($sTime);; Use Local time format settings Local $sFormat = RegRead("HKCU\Control Panel\International", "sTimeFormat") If @error Then $sFormat = "h:mm:ss tt" EndIf Local $aFormat = StringRegExp($sFormat, "\w*(.)\w*(.).*", 3) If @error Then SetError(1, 0, 0) EndIf Return StringRegExpReplace($sTime, "(\d{2}).?(\d{2}).?(\d{2})", "\1" & $aFormat[0] & "\2" & $aFormat[1] & "\3") EndFunc ;==>_TimeFormatLocal 

Return To Contents

Time Machine #1

Author: Valuater








;Time Machine #1 ;Minutes/seconds/miliseconds $Minutes = 90 ; will wait 90 minutes Local $60Count = 0, $begin = TimerInit() While $Minutes > $60Count $dif = TimerDiff($begin) $dif2 = StringLeft($dif, StringInStr($dif, ".") -1) $Count = int($dif/1000) $60Count = Int($Count / 60) ToolTip("Minutes Required = " & $Minutes & @CRLF & "Minutes Past = " & $60Count & @CRLF & "Seconds Count = " & $Count & @CRLF & "Mili-Seconds Count = " & $dif2, 20, 20, "Time Machine #1", 1) Sleep(20) WEnd MsgBox(64, "Time-Up!!", "Your " & $Minutes & " minutes have passed ") 

Return To Contents

Time Machine #2

Author: Valuater








; Time Machine #2 ; Hours & Minutes $start = @HOUR & ":" & @MIN + 2 ; for testing While 1 $start2 = @HOUR & ":" & @MIN If $start = $start2 Then Run("notepad.exe") ExitLoop EndIf ToolTip("Start Time = " & $start & @CRLF & "Real Time = " & $start2, 20, 20, "Time Machine", 1) Sleep(2000) WEnd Exit 

Return To Contents

Time Machine #3

Author: Valuater








; Time Machine #3 ; Day & Hour $start = @MDAY & "/" & @HOUR + 1 ; for testing While 1 $start2 = @MDAY & "/" & @HOUR If $start = $start2 Then Run("notepad.exe") Exit EndIf ToolTip("Start Time = " & $start & @CRLF & "Real Time = " & $start2, 120, 120, "Time Machine", 1) Sleep(2000) WEnd Exit 

Return To Contents

_GetTodaysDate

Author: guinness








ConsoleWrite(_GetTodaysDate() & @CRLF) ; Return the date and the time. ConsoleWrite(_GetTodaysDate(0) & @CRLF) ; Return the date only. Func _GetTodaysDate($iReturnTime = 1) Local $aMDay[8] = [7, "Sun", "Mon", "Tue", "Wed", "Thur", "Fri", "Sat"], _ $aMonth[13] = [12, "Jan", "Feb", "Mar", "Apr", "May", "June", "July", "Aug", "Sept", "Oct", "Nov", "Dec"], $aTime[2] = ["", ' ' & @HOUR & ':' & @MIN & ':' & '00'] Return $aMDay[@WDAY] & ', ' & @MDAY & ' ' & $aMonth[@MON] & ' ' & @YEAR & $aTime[$iReturnTime] EndFunc ;==>_GetTodaysDate 

Return To Contents