计算任意两个时间经过星期几(如:星期六)的某个时间段(如:6点-8点)的总秒数例:2004-12-17 01:00:01 到 2004-12-30  01:00:01 这两个时间经过星期六6点到8点的总秒数为:14400秒要求写一个函数可以适合任意情况的两个时间经过星期几(如:星期六)的某个时间段(如:6点-8点)的总秒数谢谢!

解决方案 »

  1.   

    写函数代码还是免了吧,我给你说说处理的方法1.循环日期,从开始日期到结束日期,
    2.判断weekday(日期,vbMonday)=6为真,读取该天在该时间范围内的秒数,累加.需要注意的是如果第一天或最后一天
      

  2.   

    Public Function Get_Seconds(Byval StartTime As Date, Byval EndTime As Date, Byval limitS As Date, Byval limitE As Date) As LongDim Tmp As Long
    Dim Seconds As LongIf StartTime >= EndTime Then Exit Function
    If limitS >= limitE Then Exit Function
    If limitS >= EndTime Or StartTime <= limitE Then Exit Function
     
    Seconds = DateDiff("s",limitS, limitE)Tmp = DateDiff("s",limitS, StartTime)
    If Tmp > 0 Then Seconds = Seconds - TmpTmp = DateDiff("s",EndTime, limitE)
    If Tmp > 0 Then Seconds = Seconds - TmpGet_Seconds = Seconds
    End Function
      

  3.   

    Option ExplicitPrivate Sub Command1_Click()
    Dim d1, d2 As Date
    Dim scd As Long
        d1 = TimeValue(#1:00:00 PM#)
        d2 = TimeValue(#2:00:01 PM#)
        
        scd = Interal(d1, d2)
        
        MsgBox scdEnd SubPrivate Function Interal(ByVal d1 As Date, ByVal d2 As Date)Dim i, j As Long
    Dim hour1, minute1, second1 As Integer
    Dim hour2, minute2, second2 As Integer
    Dim scd As Long
        hour1 = hour(d1)
        hour2 = hour(d2)
        minute1 = minute(d1)
        minute2 = minute(d2)
        second1 = second(d1)
        second2 = second(d2)
        
        If hour2 - hour1 <> 0 Then
            scd = scd + (hour2 - hour1) * 60 * 60
        End If
        
        If minute2 - minute1 <> 0 Then
            scd = scd + (minute2 - minute1) * 60
        End If
            
        scd = scd + (second2 - second1)
        
        Interal = scdEnd Function
      

  4.   

    Option ExplicitPrivate Sub Command1_Click()
        Debug.Print DateDiff("s", Text1.Text, Text2.Text)
    End SubPrivate Sub Form_Load()
        Text1.Text = Now
        Text2.Text = Now
    End Sub
      

  5.   

    谢谢楼上回复,但不是我要的答案!
    另外告诉楼上你的求两个指定日期的时间间隔用DateDiff就行了!再顶
      

  6.   

    给你个更简单的 用 datediff 函数就能完成Private Sub Command1_Click()
    Dim d1, d2 As Date
        d1 = #12/30/2004 1:00:00 PM#
        d2 = #12/31/2004 1:00:00 PM#
        
        
        
        MsgBox DateDiff("s", d1, d2)
    End Sub
      

  7.   

    计算日期之间的间隔
    <%@ page import="java.util.Date"%>
    <%@ page import="java.text.DateFormat"%>
    <%
    String input = "2003-05-01";
    SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
    Date d1 = null;
    try{
    d1 = formatter.parse(input);
    }catch(ParseException e){
    out.println("unparseable using " + formatter);
    }
    Date d2 = new Date();
    long diff = d2.getTime() - d1.getTime();
    out.println("Difference is " + (diff/(1000*60*60*24)) + " days.");
    %>
    输出结果为:
    Difference is 29 days. 
    这个是计算天数的~你改一下看看能不能计算秒
      

  8.   

    这个一定能用,已试。
    ----------------------
    Option ExplicitPrivate Sub Command1_Click()
        MsgBox IsWeekTime(Text1.Text, Text2.Text, "06:00:01", "08:00:01", 6)
    End SubPrivate Sub Form_Load()
        Text1.Text = Now
        Text2.Text = Now
    End SubPrivate Function IsWeekTime(ByVal Date1 As Date, ByVal Date2 As Date, ByVal Time1 As String, ByVal Time2 As String, ByVal InMonth As Integer) As Long
        Dim MyDate As Date
        Dim dRet As Long
        Dim tRet As Long
        Dim i As Long
        MyDate = Date1
        For i = 0 To 6
            If Weekday(MyDate, vbMonday) = InMonth Then
                Exit For
            Else
                MyDate = DateAdd("d", 1, MyDate)
            End If
        Next
        dRet = DateDiff("ww", MyDate, Date2)
        tRet = DateDiff("s", TimeValue(Time1), TimeValue(Time2))
        IsWeekTime = dRet * tRet
    End Function
      

  9.   

    懒的试了,楼主看看吧。
    Option ExplicitPrivate Sub Command1_Click()
        MsgBox IsWeekTime(Text1.Text, Text2.Text, "06:00:01", "08:00:01", 6)
    End SubPrivate Sub Form_Load()
        Text1.Text = "2004-12-17 01:00:01"
        Text2.Text = "2004-12-25 06:50:01"
    End SubPrivate Function IsWeekTime(ByVal Date1 As Date, ByVal Date2 As Date, ByVal Time1 As String, ByVal Time2 As String, ByVal InMonth As Integer) As Long
        Dim MyDate As Date
        Dim MyDate2 As Date
        Dim dRet As Long
        Dim tRet As Long
        Dim i As Long
        MyDate = Date1
        If Weekday(Date2, vbMonday) = InMonth Then
            MyDate2 = DateAdd("d", 1, Date2)
        Else
            MyDate2 = Date2
        End If
        For i = 0 To 6
            If Weekday(MyDate, vbMonday) = InMonth Then
                Exit For
            Else
                MyDate = DateAdd("d", 1, MyDate)
            End If
        Next
        dRet = DateDiff("ww", MyDate, MyDate2)
        tRet = DateDiff("s", TimeValue(Time1), TimeValue(Time2))
        
        If TimeValue(Date1) >= TimeValue(Time1) Then
            i = dRet * tRet - Abs(DateDiff("s", TimeValue(Date1), TimeValue(Time1)))
        Else
            i = dRet * tRet
        End If
        
        If TimeValue(Date2) <= TimeValue(Time2) Then
            i = i - Abs(DateDiff("s", TimeValue(Date2), TimeValue(Time2)))
        End If    IsWeekTime = i
    End Function
      

  10.   

    http://community.csdn.net/Expert/topic/3696/3696617.xml?temp=.1594049
    印尼亚齐省劫掠现象严重华人成为抢劫目标http://news.sina.com.cn/c/2005-01-03/18414704205s.shtml
    港报呼吁印尼当局制止歹徒趁乱抢掠华人恶行
      

  11.   

    Public Function WeekTimeNum(ByVal SDate As Date, ByVal EDate As Date, ByVal WeekVal As Integer, ByVal STime As String, ByVal ETime As String)
    'weekVal 表  星期一 = 1  星期二 =2
        Dim TmpDate As Date
        Dim i As Integer, j As Integer
        
        '因为weekday 计算出来的 星期一 = 2  星期天 = 1
        WeekVal = WeekVal + 1
        i = Abs(DateDiff("ww", SDate, EDate, Weekday(SDate)))
        j = Abs(DateDiff("s", TimeValue(STime), TimeValue(ETime)))
        
        If Weekday(SDate) <= WeekVal And Weekday(EDate) >= WeekVal Then
            i = i + 1
        End If
        If Weekday(SDate) < WeekVal And Weekday(EDate) < WeekVal Then
            i = i + 1
        End If
        If Weekday(SDate) > WeekVal And Weekday(EDate) > WeekVal Then
            i = i + 1
        End If
        
        WeekTimeNum = i * j
    End Function
      

  12.   

    Public Function WeekTimeNum(ByVal SDate As Date, ByVal EDate As Date, ByVal WeekVal As Integer, ByVal STime As String, ByVal ETime As String) As Long
        'weekVal 表  星期一 = 1  星期二 =2
        Dim TmpDate As Date
        Dim i As Long, j As Long
        
        '因为weekday 计算出来的 星期一 = 2  星期天 = 1
        '
        WeekVal = WeekVal + 1
        i = Abs(DateDiff("ww", SDate, EDate, Weekday(SDate)))
        j = DateDiff("s", TimeValue(STime), TimeValue(ETime))
        
        If Weekday(SDate) <= WeekVal And Weekday(EDate) >= WeekVal Then
            i = i + 1
        End If
        If Weekday(SDate) < WeekVal And Weekday(EDate) < WeekVal Then
            i = i + 1
        End If
        If Weekday(SDate) > WeekVal And Weekday(EDate) > WeekVal Then
            i = i + 1
        End If
        
        
        
        WeekTimeNum = i * j
        
        If Weekday(SDate) = WeekVal Then
            If TimeValue(SDate) > TimeValue(STime) And TimeValue(SDate) < TimeValue(ETime) Then
                   WeekTimeNum = WeekTimeNum - DateDiff("s", TimeValue(SDate), TimeValue(STime))
            End If
            If TimeValue(SDate) < TimeValue(STime) Then
                   WeekTimeNum = WeekTimeNum + j
            End If
        End If
        If Weekday(EDate) = WeekVal Then
            If TimeValue(EDate) > TimeValue(STime) And TimeValue(EDate) < TimeValue(ETime) Then
                   WeekTimeNum = WeekTimeNum - DateDiff("s", TimeValue(EDate), TimeValue(ETime))
            End If
            If TimeValue(EDate) < TimeValue(STime) Then
                   WeekTimeNum = WeekTimeNum - j
            End If
        End If
        
    End Function
      

  13.   

    Public Function WeekTimeNum(ByVal SDate As Date, ByVal EDate As Date, ByVal WeekVal As Integer, ByVal STime As String, ByVal ETime As String) As Long
        'weekVal 表  星期一 = 1  星期二 =2
        Dim TmpDate As Date
        Dim i As Long, j As Long
        
        '因为weekday 计算出来的 星期一 = 2  星期天 = 1
        '
        WeekVal = WeekVal + 1
        i = Abs(DateDiff("ww", SDate, EDate, Weekday(SDate)))
        j = DateDiff("s", TimeValue(STime), TimeValue(ETime))
        
        If Weekday(SDate) <= WeekVal And Weekday(EDate) >= WeekVal Then
            i = i + 1
        End If
        If Weekday(SDate) < WeekVal And Weekday(EDate) < WeekVal Then
            i = i + 1
        End If
        If Weekday(SDate) > WeekVal And Weekday(EDate) > WeekVal Then
            i = i + 1
        End If
        
        
        
        WeekTimeNum = i * j
        
        If Weekday(SDate) = WeekVal Then
            If TimeValue(SDate) > TimeValue(STime) And TimeValue(SDate) < TimeValue(ETime) Then
                   WeekTimeNum = WeekTimeNum - Abs(DateDiff("s", TimeValue(SDate), TimeValue(STime)))
            End If
            If TimeValue(SDate) < TimeValue(STime) And DateValue(SDate) <> DateValue(EDate) Then
                   WeekTimeNum = WeekTimeNum + j
            End If
        End If
        If Weekday(EDate) = WeekVal Then
            If TimeValue(EDate) > TimeValue(STime) And TimeValue(EDate) < TimeValue(ETime) Then
                   WeekTimeNum = WeekTimeNum - DateDiff("s", TimeValue(EDate), TimeValue(ETime))
            End If
            If TimeValue(EDate) < TimeValue(STime) Then
                   WeekTimeNum = WeekTimeNum - j
            End If
        End If
        
    End Function
      

  14.   

    你认真看了吗?真的不可以?
    Private Sub Command1_Click()
       MsgBox WeekTimeNum("2004-12-17 01:00:01", "2004-12-25 06:50:01", 6, "06:00:01", "08:00:01")
    End SubPublic Function WeekTimeNum(ByVal SDate As Date, ByVal EDate As Date, ByVal WeekVal As Integer, ByVal STime As String, ByVal ETime As String) As Long
        'weekVal 表  星期一 = 1  星期二 =2
        Dim TmpDate As Date
        Dim i As Long, j As Long
        
        '因为weekday 计算出来的 星期一 = 2  星期天 = 1
        '
        WeekVal = WeekVal + 1
        i = Abs(DateDiff("ww", SDate, EDate, Weekday(SDate)))
        j = DateDiff("s", TimeValue(STime), TimeValue(ETime))
        
        If Weekday(SDate) <= WeekVal And Weekday(EDate) >= WeekVal Then
            i = i + 1
        End If
        If Weekday(SDate) < WeekVal And Weekday(EDate) < WeekVal Then
            i = i + 1
        End If
        If Weekday(SDate) > WeekVal And Weekday(EDate) > WeekVal Then
            i = i + 1
        End If
        
        
        
        WeekTimeNum = i * j
        
        If Weekday(SDate) = WeekVal Then
            If TimeValue(SDate) > TimeValue(STime) And TimeValue(SDate) < TimeValue(ETime) Then
                   WeekTimeNum = WeekTimeNum - Abs(DateDiff("s", TimeValue(SDate), TimeValue(STime)))
            End If
            If TimeValue(SDate) < TimeValue(STime) And DateValue(SDate) <> DateValue(EDate) Then
                   WeekTimeNum = WeekTimeNum + j
            End If
        End If
        If Weekday(EDate) = WeekVal Then
            If TimeValue(EDate) > TimeValue(STime) And TimeValue(EDate) < TimeValue(ETime) Then
                   WeekTimeNum = WeekTimeNum - DateDiff("s", TimeValue(EDate), TimeValue(ETime))
            End If
            If TimeValue(EDate) < TimeValue(STime) Then
                   WeekTimeNum = WeekTimeNum - j
            End If
        End If
        
    End Function
      

  15.   

    你再试一下。Private Sub Command1_Click()
       MsgBox WeekTimeNum("2004-12-17 01:00:01", "2004-12-25 06:50:01", 6, "06:00:01", "08:00:01")
    End SubPublic Function WeekTimeNum(ByVal SDate As Date, ByVal EDate As Date, ByVal WeekVal As Integer, ByVal STime As String, ByVal ETime As String) As Long
        'weekVal 表  星期一 = 1  星期二 =2
        Dim TmpDate As Date
        Dim i As Long, j As Long
        
        '因为weekday 计算出来的 星期一 = 2  星期天 = 1
        '
        WeekVal = WeekVal + 1
        i = Abs(DateDiff("ww", SDate, EDate, Weekday(SDate)))
        j = DateDiff("s", TimeValue(STime), TimeValue(ETime))
        
        If Weekday(SDate) <= WeekVal And Weekday(EDate) >= WeekVal Then
            i = i + 1
        End If
        If i > 1 Then
            If Weekday(SDate) < WeekVal And Weekday(EDate) < WeekVal Then
                i = i + 1
            End If
            If Weekday(SDate) > WeekVal And Weekday(EDate) > WeekVal Then
                i = i + 1
            End If
        End If
        
        
        WeekTimeNum = i * j
        
        If Weekday(SDate) = WeekVal Then
            If TimeValue(SDate) > TimeValue(STime) And TimeValue(SDate) < TimeValue(ETime) Then
                   WeekTimeNum = WeekTimeNum - Abs(DateDiff("s", TimeValue(SDate), TimeValue(STime)))
            End If
            If TimeValue(SDate) < TimeValue(STime) And DateValue(SDate) <> DateValue(EDate) Then
                   WeekTimeNum = WeekTimeNum + j
            End If
        End If
        If Weekday(EDate) = WeekVal Then
            If TimeValue(EDate) > TimeValue(STime) And TimeValue(EDate) < TimeValue(ETime) Then
                   WeekTimeNum = WeekTimeNum - DateDiff("s", TimeValue(EDate), TimeValue(ETime))
            End If
            If TimeValue(EDate) < TimeValue(STime) Then
                   WeekTimeNum = WeekTimeNum - j
            End If
        End If
        
    End Function
      

  16.   

    'WeekVal:0——星期天;1——星期一;……6——星期六
    Private Function WeekTimeNum(ByVal SDate As Date, ByVal EDate As Date, _
                    ByVal WeekVal As Long, ByVal STime As Date, ByVal ETime As Date) As Long
        Dim SecDiff As Single
        Dim lngDayDiff As Long
        Dim SWeekVal As Long
        Dim iRet As Long
        
        If WeekVal >= 0 Or WeekVal <= 6 Then
            STime = STime - Int(STime)
            ETime = ETime - Int(ETime)
            If ETime > STime Then
                SecDiff = (ETime - STime) * 86400
                
                lngDayDiff = Int(EDate) - Int(SDate)
                SWeekVal = Weekday(SDate, vbSunday) - 1
                If lngDayDiff > 0 Then
                    lngDayDiff = lngDayDiff - (WeekVal - SWeekVal)
                    If WeekVal < SWeekVal Then lngDayDiff = lngDayDiff - 7
                    If lngDayDiff > 0 Then iRet = SecDiff * (((lngDayDiff - 1) \ 7) + 1)
                End If
                
                If SWeekVal = WeekVal Then
                    SDate = SDate - Int(SDate)
                    If SDate >= ETime Then
                        iRet = iRet - SecDiff
                    ElseIf SDate > STime Then
                        iRet = iRet - (SDate - STime) * 86400
                    End If
                End If
                
                If Weekday(EDate, vbSunday) - 1 = WeekVal Then
                    EDate = EDate - Int(EDate)
                    If EDate >= ETime Then
                        iRet = iRet + SecDiff
                    ElseIf EDate > STime Then
                        iRet = iRet + (EDate - STime) * 86400
                    End If
                End If
            End If
        End If
        WeekTimeNum= iRet
    End Function
      

  17.   

    不好意思,上面“If WeekVal >= 0 Or WeekVal <= 6 Then”
    应该改为“If WeekVal >= 0 And WeekVal <= 6 Then”