时间的相加和相减

来源:互联网 发布:0x80072ee2激活windows 编辑:程序博客网 时间:2024/05/27 20:34
  '过程名称:TimeAndTimeSum
  '功能描述:两个时间的相加
  '接收参数:时间字串
  '返回参数:相加后的时间字串
  '创建人员及日期:zzz@2007-06-20
  '注意事项:字符串的格式需要为##:##
  '================================================================================================
  Public Function TimeAndTimeSum(ByVal fristTime As String, ByVal SecondTime As StringAs String
    
Dim fristHour As Integer, fristMinute As Integer, secondHour As Integer, secondMinute As Integer
    
Dim HourSum As Integer, MinuteSum As Integer

    Try
      
'得到fristTime的小时和分钟
      fristHour = Convert.ToInt32(fristTime.Substring(0, fristTime.IndexOf(":")))
      fristMinute 
= Convert.ToInt32(fristTime.Substring(fristTime.IndexOf(":"+ 12))

      
'得到SecondTime的小时和分钟
      secondHour = Convert.ToInt32(SecondTime.Substring(0, SecondTime.IndexOf(":")))
      secondMinute 
= Convert.ToInt32(SecondTime.Substring(SecondTime.IndexOf(":"+ 12))

      
'小时和分钟数求和
      HourSum = fristHour + secondHour
      MinuteSum 
= fristMinute + secondMinute

      
'判断分钟数是否大于等于60,如果是在小是数上加1
      If MinuteSum < 60 Then
        
If MinuteSum Mod 60 < 10 Then
          TimeAndTimeSum 
= HourSum & ":0" & MinuteSum Mod 60
        
Else
          TimeAndTimeSum 
= HourSum & ":" & MinuteSum Mod 60
        
End If
      
Else
        
If MinuteSum Mod 60 < 10 Then
          TimeAndTimeSum 
= HourSum + 1 & ":0" & MinuteSum Mod 60
        
Else
          TimeAndTimeSum 
= HourSum + 1 & ":" & MinuteSum Mod 60
        
End If
      
End If
    Catch ex 
As Exception
      Throw 
New Exception("时间的累加出现异常!!!" & vbCrLf & _
                          
"Source:" & ex.Source.ToString() & "Message:" & ex.Message)
      TimeAndTimeSum 
= ""
    
End Try
  
End Function

  
'过程名称:TimeAndTimeReduces
  '功能描述:两个时间的相加
  '接收参数:fristTime:被减时间,SecondTime:减去的时间
  '返回参数:相减后的时间字串
  '创建人员及日期:zzz@2007-06-20
  '注意事项:字符串的格式需要为##:##
  '================================================================================================
  Public Function TimeAndTimeReduces(ByVal fristTime As String, ByVal SecondTime As StringAs String
    
Dim fristHour As Integer, fristMinute As Integer, secondHour As Integer, secondMinute As Integer
    
Dim HourSum As Integer, MinuteSum As Integer, sTemp As String = "", isJh As Boolean = False

    Try
      
If fristTime.Length = SecondTime.Length Then
        
If fristTime < SecondTime Then
          sTemp 
= fristTime
          fristTime 
= SecondTime
          SecondTime 
= sTemp
          isJh 
= True
        
End If
      
Else
        
If Convert.ToInt32(fristTime.Substring(0, fristTime.IndexOf(":"))) < Convert.ToInt32(SecondTime.Substring(0, SecondTime.IndexOf(":"))) Then
          sTemp 
= fristTime
          fristTime 
= SecondTime
          SecondTime 
= sTemp
          isJh 
= True
        
End If
      
End If

      
'得到fristTime的小时和分钟
      fristHour = Convert.ToInt32(fristTime.Substring(0, fristTime.IndexOf(":")))
      fristMinute 
= Convert.ToInt32(fristTime.Substring(fristTime.IndexOf(":"+ 12))

      
'得到SecondTime的小时和分钟
      secondHour = Convert.ToInt32(SecondTime.Substring(0, SecondTime.IndexOf(":")))
      secondMinute 
= Convert.ToInt32(SecondTime.Substring(SecondTime.IndexOf(":"+ 12))

      
'小时和分钟数相减
      HourSum = fristHour - secondHour
      
If fristMinute < secondMinute Then
        
If HourSum = 0 Then
        
Else
          HourSum 
-= 1
        
End If

        MinuteSum 
= fristMinute + 60 - secondMinute
        MinuteSum 
= -MinuteSum + 60
      
Else
        MinuteSum 
= fristMinute - secondMinute
      
End If

      
'判断分钟数是否小于10
      If MinuteSum < 10 Then
        
If isJh Then
          TimeAndTimeReduces 
= "-" & Math.Abs(HourSum) & ":0" & Math.Abs(MinuteSum)
        
Else
          TimeAndTimeReduces 
= HourSum & ":0" & Math.Abs(MinuteSum)
        
End If
      
Else
        
If isJh Then
          TimeAndTimeReduces 
= "-" & Math.Abs(HourSum) & ":" & Math.Abs(MinuteSum)
        
Else
          TimeAndTimeReduces 
= HourSum & ":" & Math.Abs(MinuteSum)
        
End If
      
End If

    Catch ex 
As Exception
      Throw 
New Exception("时间的相减出现异常!!!" & vbCrLf & _
                          
"Source:" & ex.Source.ToString() & "Message:" & ex.Message)
      TimeAndTimeReduces 
= ""
    
End Try
  
End Function