本帖最后由 xyhqqaa 于 2012-5-14 16:19 编辑
新手求助, 计算选定时间与当前时间的时间差。#include <ButtonConstants.au3>
#include <DateTimeConstants.au3>
#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
#include <Date.au3>
#Region ### START Koda GUI section ### Form=
$Form1 = GUICreate("Form1", 421, 168, 346, 349)
$Date1 = GUICtrlCreateDate(@YEAR&"/"&@MON&"/"&@MDAY, 112, 48, 265, 25)
$Button1 = GUICtrlCreateButton("计算", 192, 88, 75, 25)
$Label1 = GUICtrlCreateLabel("Label1", 176, 128, 212, 17)
$Label2 = GUICtrlCreateLabel("时间差:", 112, 128, 52, 17)
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###
While 1
$nMsg = GUIGetMsg()
Switch $nMsg
Case $GUI_EVENT_CLOSE
Exit
Case $Button1
$read=GUICtrlRead($Date1)
$iDateCalc = _DateDiff('h', $read, _NowCalc())
GUICtrlSetData($Label1,$iDateCalc)
EndSwitch
WEnd
然后贴上坛子找的某大哥写的,但是看不懂。。有没有比较简单的实现方法没??????或者能稍微讲解下下面某段的意思吗
#include <Date.au3>
Local $sStartDate = "1/1/2008" ; "" ;
Local $sStartTime = "11:30AM"
Local $sEndDate = @YEAR & "/" & @MON & "/" & @MDAY ; "" ;
Local $sEndTime = "3:00PM"
$sStart = _Date_Time_Convert($sStartDate & $sStartTime, "d/M/yyyyhh:mmtt") ; "hh:mmtt") ;
ConsoleWrite("$sStart = " & $sStart & @CRLF)
$sEnd = _Date_Time_Convert($sEndDate & $sEndTime, "dd/MM/yyyyh:mmtt") ; "h:mmtt") ;
ConsoleWrite("$sEnd = " & $sEnd & @CRLF)
$iDateCalcD = _DateDiff('D', $sStart, $sEnd)
$iDateCalch = _DateDiff('h', $sStart, $sEnd)
$iDateCalcm = _DateDiff('n', $sStart, $sEnd)
MsgBox(0, "小时差", $sStart & @LF & $sEnd & @LF & "相差: " & _DateDiff("h", $sStart, $sEnd) & "小时")
MsgBox(4096, "时间差:", $sStartDate & " 距离今天 " & $iDateCalcD & " 天" & Int($iDateCalch - $iDateCalcD * 24) & " 小时" & Int($iDateCalcm - $iDateCalch * 60) & "分钟")
Func _Date_Time_Convert($sDateTime, $sDateTimeFormat, $sRetFormat = "yyyy/MM/dd HH:mm:ss")
Local $Time, $iYear, $iMnth, $iDay, $iHour, $iMinute, $iSec, $aFormatIn, $msg
Local $aMMM[12] = ["jan", "feb", "mar", "apr", "may", "jun", "jul", "aug", "sep", "oct", "nov", "dec"]
$aFormatIn = StringRegExp($sDateTimeFormat, "(y+|M+|d+|H+|h+|m+|s+|t+)", 3) ;Array of format characters.
For $i = 0 To UBound($aFormatIn) - 1
$msg = StringLeft($aFormatIn[$i], 1)
Select
Case $msg = "y" ; y - Year
If StringLen($aFormatIn[$i]) = 4 Then _
$iYear = StringMid($sDateTime, StringInStr($sDateTimeFormat, "yyyy"), _
StringLen($aFormatIn[$i])) & "/"
If StringLen($aFormatIn[$i]) = 2 Then
$iYear = StringMid($sDateTime, StringInStr($sDateTimeFormat, "yy"), 2)
If Number($iYear) < Number(StringRight(@YEAR, 2) + 20) Then
$iYear = "20" & $iYear & "/"
Else
$iYear = "19" & $iYear & "/"
EndIf
EndIf
Case StringRegExp($msg, "M") ; M - Month
If StringLen($aFormatIn[$i]) > 2 Then
$iMnth = StringMid($sDateTime, StringInStr($sDateTimeFormat, "MMM", 1), 3)
If StringLen($aFormatIn[$i]) < 3 Then _
$iMnth = StringMid($sDateTime, StringInStr($sDateTimeFormat, "MMM", 1) + 1, 2)
$sDateTime = StringRegExpReplace($sDateTime, "\b(" & $iMnth & ".*?)\b", $iMnth)
$sDateTimeFormat = StringRegExpReplace($sDateTimeFormat, "\b(M{3})(M*)\b", "\1")
EndIf
If StringLen($aFormatIn[$i]) > 2 Then
For $j = 0 To UBound($aMMM) - 1
If StringLeft($iMnth, 3) = $aMMM[$j] Then $iMnth = StringRight("0" & $j + 1, 2) & "/"
Next
ElseIf StringLen($aFormatIn[$i]) < 3 Then
$iMnth = Number(StringMid($sDateTime, StringInStr($sDateTimeFormat, "M", 1), 2))
If $iMnth > 9 And StringLen($aFormatIn[$i]) = 1 Then $sDateTimeFormat = _
StringRegExpReplace($sDateTimeFormat, "(?:\b|[^M])(M)(?:\b|[^M])", " MM")
$iMnth = StringRight("0" & $iMnth, 2) & "/"
EndIf
Case $msg = "d" ; d - Day
If StringLen($aFormatIn[$i]) > 2 Then
$iDay = StringMid($sDateTime, StringInStr($sDateTimeFormat, "ddd", 1), 3)
$sDateTime = StringRegExpReplace($sDateTime, "\b(" & $iDay & ".*?)\b", "")
$sDateTimeFormat = StringRegExpReplace($sDateTimeFormat, "\b(d{3})(d*)\b", "")
EndIf
If StringLen($aFormatIn[$i]) <= 2 Then
$iDay = Number(StringMid($sDateTime, StringInStr($sDateTimeFormat, "d", 1), 2))
If $iDay > 9 And StringLen($aFormatIn[$i]) = 1 Then $sDateTimeFormat = _
StringRegExpReplace($sDateTimeFormat, "(?:\b|[^d])(d)(?:\b|[^d])", " dd")
EndIf
$iDay = StringRight("0" & $iDay, 2) & " "
Case StringRegExp($msg, "(?i)h") ; H or h - Hour
If StringInStr($sDateTimeFormat, "H", 1) Then
$iHour = StringRight("0" & Number(StringMid($sDateTime, _
StringInStr($sDateTimeFormat, "H", 1), 2)), 2)
ElseIf StringInStr($sDateTimeFormat, "h", 1) Then
$iHour = Number(StringMid($sDateTime, StringInStr($sDateTimeFormat, "h", 1), 2))
EndIf
If StringInStr($sDateTime, "AM") And $iHour = 12 Then $iHour = 0
If StringInStr($sDateTime, "PM") And $iHour < 12 Then $iHour += 12
If $iHour > 9 And StringLen($aFormatIn[$i]) = 1 Then $sDateTimeFormat = _
StringRegExpReplace($sDateTimeFormat, "(?i)(?:\b|[^h])(h)(?:\b|[^h])", " hh")
$iHour = StringRight("0" & $iHour, 2) & ":"
Case StringRegExp($msg, "m") ; m - Minute
If StringInStr($sDateTimeFormat, "m", 1) Then
$iMinute = Number(StringMid($sDateTime, StringInStr($sDateTimeFormat, "m", 1), 2))
EndIf
If $iMinute > 9 And StringLen($aFormatIn[$i]) = 1 Then $sDateTimeFormat = _
StringRegExpReplace($sDateTimeFormat, "(?:\b|[^m])(m)(?:\b|[^m])", " mm")
$iMinute = StringRight("0" & $iMinute, 2) & ":"
Case $msg = "s" ; s - Second
If StringInStr($sDateTimeFormat, "ss") Then
$iSec = StringMid($sDateTime, StringInStr($sDateTimeFormat, "ss"), 2)
EndIf
If $iSec > 9 And StringLen($aFormatIn[$i]) = 1 Then $sDateTimeFormat = _
StringRegExpReplace($sDateTimeFormat, "(?:\b|[^s])(s)(?:\b|[^s])", " ss")
$iSec = StringRight("0" & $iSec, 2) & ":"
EndSelect
Next
; Default values added to empty, unused variables for entry into the Date Control.
If $iYear = "" Then $iYear = "1900/"
If $iMnth = "" Then $iMnth = "01/"
If $iDay = "" Then $iDay = "01 "
If $iHour = "" Then $iHour = "00:"
If $iMinute = "" Then $iMinute = "00:"
If $iSec = "" Then $iSec = "00"
$Time = $iYear & $iMnth & $iDay & $iHour & $iMinute & $iSec
;===== The following converts $Time to $sRetFormat format usind Date Control ======
; $Time is now in this format "yyyy/MM/dd HH:mm:ss"
Local $hGui = GUICreate("My GUI get date", 200, 200, 800, 200)
Local $idDate = GUICtrlCreateDate($Time, 10, 10, 185, 20)
GUICtrlSendMsg($idDate, 0x1032, 0, $sRetFormat)
Local $sReturn = GUICtrlRead($idDate)
GUIDelete($hGui)
Return $sReturn
EndFunc ;==>_Date_Time_Convert
|