does anyone know how to subtract a certain number of days from a given date?
thanks in advance for your help.

Nevermind I figured it out with the help of the help index.

13 Years
Discussion Span
Last Post by marc-0

I know nothing about vb.net (sure sounds like i can help :rolleyes: )
but maybe this will work

Public Function TimeDiff(LesserTime As String, LesserTimeDate As String, GreaterTime As String, GreaterTimeDate As String) As String

Dim Thrs As Single, Tmins As Single, Tsecs As Single, ReturnTime As String
Dim DTcheck As Single
DTcheck = DateDiff("d", LesserTimeDate, GreaterTimeDate)

If DTcheck > 0 Then
  For i = 0 To DTcheck
    Hour(GreaterTime) = Hour(GreaterTime) + 24
 ElseIf DTcheck < 0 Then
  TimeDiff = "ERROR"
  Exit Function
End If
Thrs = Hour(GreaterTime) - Hour(LesserTime)
If Thrs > 0 Then
  Tmins = Minute(GreaterTime) - Minute(LesserTime)
  If Tmins < 0 Then
    Tmins = Tmins + 60
    Thrs = Thrs - 1
  End If
 ElseIf Thrs = 0 And Minute(GreaterTime) > Minute(LesserTime) Then
   Tmins = Minute(GreaterTime) - Minute(LesserTime)
   TimeDiff = "ERROR"
   Exit Function
End If
Tsecs = Second(GreaterTime) - Second(LesserTime)
If Tsecs < 0 Then
  Tsecs = Tsecs + 60
  If Tmins > 0 Then Tmins = Tmins - 1 Else Tmins = 59
End If

TimeDiff = TimeSerial(Thrs, Tmins, Tsecs)

End Function

It's not a big issue to subtract date from given days.You can use the "Subtract" function in ASP.NET.

E.g :-
DateTime dt=DateTime.Now;
TimeSpan span=dt.Date.Subtract(dt.AddDays(-5));

Himanshu Sharma


Here is a routine to add 24 hours to the current date/time. Maybe it will help.

Dim futureTime As TimeSpan = ExecTime - Now

'if the calculated time has already passed,
'add 24 hours so it does it tomorrow

If waitTime.Hours < 0 Then
Dim myTimeSpan As New TimeSpan(24, 0, 0)
futureTime = futureTime .Add(myTimeSpan)
End If

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.