cem
cem

Reputation:

Date Calculating

Can u give me a simple function in vb.net which can take number of days and date as parameters and subtract these number of days from given date. Forexample

Private function Calculate(Byval p_number_days,p_date) as date

 Dim calculated_date as Date= (p_date) - (p_number_days) 
 return calculated


End Function

Upvotes: 0

Views: 178

Answers (4)

Jonathan Allen
Jonathan Allen

Reputation: 70307

If you can, always represent number of days as a TimeSpan. It makes the code look much nicer.

Private function Calculate(Byval p_number_days as TimeSpan,p_date as Date) as date
    Return p_date - p_number_days

Upvotes: 0

Ray Hidayat
Ray Hidayat

Reputation: 16249

You'll have to excuse me, I haven't used VB in a long time, so some syntax might be wrong.

Private Function Calculate(p_days AS Integer, p_date AS Date) AS Date
  Return p_date - TimeSpan.FromDays(p_days)
End Function

Upvotes: 0

Adam Ralph
Adam Ralph

Reputation: 29956

Just use the AddDays() method of the DateTime class - MSDN

It can take a negative value as well as positive.

Upvotes: 0

Joel Coehoorn
Joel Coehoorn

Reputation: 415690

Private Function Calculate(ByVal p_Number_Days As Integer, ByVal p_Date As DateTime) As DateTime
    Return p_Date.AddDays(p_Number_Days * -1)
End Function

Upvotes: 2

Related Questions