时间的相加和相减

  '过程名称: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
 
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值