找回密码
 加入
搜索
查看: 10530|回复: 7

[AU3基础] 计算选定时间与当前系统时间的时间差。

  [复制链接]
发表于 2012-4-26 11:47:58 | 显示全部楼层 |阅读模式
悬赏10金钱已解决
本帖最后由 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
附件: 您需要 登录 才可以下载或查看,没有账号?加入

最佳答案

查看完整内容

[au3] #include #include #include #include #include #include #Region ### START Koda GUI section ### Form= $Form1 = GUICreate("Form1", 470, 290, 192, 114) $Date1 = GUICtrlCreateDate("", 72, 72, 153, 25,$DTS_SHORTDATEFORMAT) $Button1 = GUICtrlCreateButton("计算", 288, 72, 65, 33) $Label1 = GUICtrlCreateLabel("时间差:", 80, 144, 52, 17) $Label2 = GUICtrlCreateLabel("", 152, 144, 36, 12) $Label ...
发表于 2012-4-26 11:47:59 | 显示全部楼层

#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", 470, 290, 192, 114)
$Date1 = GUICtrlCreateDate("", 72, 72, 153, 25,$DTS_SHORTDATEFORMAT)
$Button1 = GUICtrlCreateButton("计算", 288, 72, 65, 33)
$Label1 = GUICtrlCreateLabel("时间差:", 80, 144, 52, 17)
$Label2 = GUICtrlCreateLabel("", 152, 144, 36, 12)
$Label3 = GUICtrlCreateLabel("天", 216, 144, 32, 17)
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###

While 1
        $nMsg = GUIGetMsg()
        Switch $nMsg
                Case $GUI_EVENT_CLOSE
                        Exit
                Case $Button1
                        $str = GUICtrlRead($Date1)
                        StringReplace($str, "-", "/")
                        GUICtrlSetData($Label2, _DateDiff('D', _NowCalc(), $str))
        EndSwitch
WEnd

评分

参与人数 1金钱 +10 贡献 +8 收起 理由
xyhqqaa + 10 + 8 谢谢。。。。StringReplace。。。学习了

查看全部评分

发表于 2012-4-26 11:51:49 | 显示全部楼层
date的UDF里有计算时间差的函数

评分

参与人数 1贡献 +1 收起 理由
xyhqqaa + 1

查看全部评分

 楼主| 发表于 2012-4-26 12:01:30 | 显示全部楼层
回复 2# my788522
看来,我还是多看看里面的一些参数,看能不能自己解决。。。老求人学不深也记不牢。。先查查。明天再看看大哥们的见解
发表于 2012-4-26 12:35:29 | 显示全部楼层
回复 3# xyhqqaa
苟同,me too!

评分

参与人数 1贡献 +1 收起 理由
xyhqqaa + 1

查看全部评分

发表于 2012-4-26 12:42:12 | 显示全部楼层
参考_DateDiff

评分

参与人数 1贡献 +1 收起 理由
xyhqqaa + 1

查看全部评分

发表于 2012-4-26 15:53:42 | 显示全部楼层
返回两个日期的不同, 使用指定类型表达.

#Include <Date.au3>
_DateDiff($sType, $sStartDate, $sEndDate)




参数

$sType 下方一个值:
D = 按天数比较不同
M = 按月数比较不同
Y = 按年数比较不同
w = 按周数比较不同
h = 按小时比较不同
n = 按分钟比较不同
s = 按秒数比较不同
$sStartDate 输入开始日期,格式 "YYYY/MM/DD[ HH:MM:SS]"
$sEndDate 输入结束日期,格式 "YYYY/MM/DD[ HH:MM:SS]"



返回值

成功: 返回两个日期的经历时间.
失败: 返回 0 并设置 @Error
@error: 0 - 无错误
1 - 无效 $sType
2 - 无效 $sStartDate
3 - 无效 $sEndDate

评分

参与人数 1贡献 +1 收起 理由
xyhqqaa + 1

查看全部评分

发表于 2013-10-29 21:24:25 | 显示全部楼层
计算两个时间差,可能你是想从数据库里读出来,然后可能还是文本还是date格式?
参考代码:
#include <Date.au3>
$D="2012-12-3"
$iDateCalc = _DateDiff( 'D',_NowCalc(),$D)
MsgBox( 4096, "", "相差: " & $iDateCalc&" 天!" )
您需要登录后才可以回帖 登录 | 加入

本版积分规则

QQ|手机版|小黑屋|AUTOIT CN ( 鲁ICP备19019924号-1 )谷歌 百度

GMT+8, 2024-5-10 19:22 , Processed in 0.084502 second(s), 22 queries .

Powered by Discuz! X3.5 Licensed

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表