Subtract a date

Hi, I'm creating a aplication whith Visual Basic and I need to subtract dates, now I'm using a simple method:

Subtract a Month:

If optmes.Value = True Then
(DateValue(Now) - 30) & " " & TimeValue(Now)

Subtract a Year:

If optano.Value = True Then
(DateValue(Now) - 365) & " " & TimeValue(Now)

But this method doesn't work correctly... if I calculate a 6 month period I get a wrong date.

Can you help me with this?

[834 byte] By [FábioNobre] at [2008-1-9]
# 1
Use the DateAdd function


eperales at 2007-10-2 > top of Msdn Tech,Microsoft ISV Community Center Forums,Visual Basic for Applications (VBA)...
# 2

Hi Fábio Nobre,

Add

One button

to a Form to try this code.

Try this which adds years, type a negative number to subtract.>>>>

Code Snippet

Public Class Form1

'This highlighted line should be one line of code in your code window.>>>>

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

Dim Msg, Number As String

Dim Months As Double

Dim StartDate, SecondDate As Date

StartDate = Now

Dim IntervalType As DateInterval

IntervalType = DateInterval.Year

SecondDate = StartDate

Do

Number = InputBox("Enter number of years to add please.", "Years to add.")

Loop Until IsNumeric(Number) = True

Months = Double.Parse(Number)

Msg = "New date: " & DateAdd(IntervalType, Months, SecondDate).ToString

MessageBox.Show(Msg)

End Sub

End Class


Use this version for months.>>

Code Snippet

Public Class Form1

'This highlighted line should be one line of code in your code window.>>>>

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

Dim Msg, Number As String

Dim Months As Double

Dim StartDate, SecondDate As Date

StartDate = Now

Dim IntervalType As DateInterval

IntervalType = DateInterval.Month

SecondDate = StartDate

Do

Number = InputBox("Enter number of months to add please.", "Months to add.")

Loop Until IsNumeric(Number) = True

Months = Double.Parse(Number)

Msg = "New date: " & DateAdd(IntervalType, Months, SecondDate).ToString

MessageBox.Show(Msg)

End Sub

End Class

Regards,

S_DS

# 3

Hi,

Add

One button

to a Form to try this code.>>>>

You can also Add minus 6 months.>>>>

Code Snippet

Public Class Form1

'This highlighted line should be one line of code in your code window.>>>>

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

My.Application.ChangeCulture("en-US")

'Repesents Midnight on Christmas Day.

Dim aDate As Date = CDate("12/25/2007 00:00:00")

'Add -6 months to subtract 6 months.

aDate = aDate.AddMonths(-6)

MessageBox.Show(aDate.ToLongDateString & " " & aDate.ToLongTimeString)

End Sub

End Class

Regards,

S_DS

# 4
Thanks for the help, this code is working but only on Vb.net, and i need to use a code for Visual Basic in Microsoft Excel, because this aplication works with formulas in Excel with a aplication to connect it to a server (PI Server).

I'm using this code:

Private Sub okdata_Click()
Dim dater As Integer

If optano.Value = True Then

datafim.Text = Now

datain.Text = (DateValue(Now) - 365) & " " & TimeValue(Now)

ElseIf optsemestre.Value = True Then

datafim.Text = Now

datain.Text = (DateValue(Now) - 180) & " " & TimeValue(Now)


ElseIf optmes.Value = True Then


datafim.Text = Now

datain.Text = (DateValue(Now) - 30) & " " & TimeValue(Now)


ElseIf optsemana.Value = True Then

datafim.Text = Now

datain.Text = (DateValue(Now) - 7) & " " & TimeValue(Now)

End If
End Sub

These options are Radio Buttons, the user sellect one of these to define the time range a time
Or select another one named: "optperiodo" to manual-define the time range.

This timerange is used to calculate the number of hours that one industrial machine works inside of this range...

Can you help me? :\

FábioNobre at 2007-10-2 > top of Msdn Tech,Microsoft ISV Community Center Forums,Visual Basic for Applications (VBA)...
# 5
DateAdd is also available in VBA
eperales at 2007-10-2 > top of Msdn Tech,Microsoft ISV Community Center Forums,Visual Basic for Applications (VBA)...
# 6
ok I'm trying It now Smile
Thanks
FábioNobre at 2007-10-2 > top of Msdn Tech,Microsoft ISV Community Center Forums,Visual Basic for Applications (VBA)...