从DateTime减去小时和分钟不起作用

时间:2015-03-31 14:14:25

标签: vb.net winforms datetime

我试图从DateTime变量中减去小时和分钟,我发现其他帖子显示你应该能够使用.AddHours(-3)来实现这一点,但它对我不起作用。我从vb.net中的DateTimePicker控件获取日期时间。说时间是上午10点,我想从中减去3个小时到7点AM。我的小时变量评估为-3,但即使我只是将数字-3放在.AddHours中,它仍然不会减去时间。继承人的代码

Dim ApptTime As DateTime = Convert.ToDateTime(DateTimePicker2.Value)
        Dim travelTime As String = Label60.Text
        Dim newtime As Double
        Dim split() As String = travelTime.Split(" ")
        If split.Length = 2 Then
            Dim Minutes As String = split(0).Replace("mins", "")
        Else
            Dim Hours As Double = split(0).Replace("Hours", "")
            Dim Minutes As Double = split(2).Replace("mins", "")
            Hours = -Hours
            Minutes = -Minutes
            ApptTime.AddHours(Hours)
            ApptTime.AddMinutes(Minutes)
            Label62.Text = (ApptTime.ToString)

2 个答案:

答案 0 :(得分:1)

这是一个简单的错误......

Dim ApptTime As DateTime = Now
        'ApptTime.Subtract(New TimeSpan(1, 60, 60)) 'won't work
        ApptTime = ApptTime.Subtract(New TimeSpan(1, 60, 60)) '1h , 60m , 60s

答案 1 :(得分:1)

试试这个:

    Dim NowMinusThreeHours = DateAdd(DateInterval.Hour, -3, Now)