Click here to Skip to main content
15,891,657 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I'm using VB in visual studio 2008. Anyone know what are the codes to subtracting 2 datetimepickers to determine the number of days? i'm doing a project for employee leave management system. And it is essential that i count how many days the employee has take a leave. I need to fill an empty textbox with the no. of days.

Thanks in advance.
Posted

1 solution

Try this:
C#
DateTime beginDate = DtpDateRetour.SelectedDate;
DateTime endDate = DtpDateLocation.SelectedDate;
dateDifference = beginDate.Subtract(endDate);


For more refer:
http://msdn.microsoft.com/en-us/library/system.datetime.compare.aspx[^]

Similar discussion:
how to calculate difference between two date[^]
 
Share this answer
 
v2
Comments
Rickysay 10-Jul-12 3:02am    
How do i insert the code onto my Private Sub btn_Click?
Rickysay 10-Jul-12 3:16am    
I want to use this on Sub procedure not using void commands.
Prasad_Kulkarni 10-Jul-12 3:33am    
What is the problem? What is the problem in code. dateDifference = beginDate.Subtract(endDate); use this code on your button click which will give you date difference.
Rickysay 10-Jul-12 3:36am    
Private Sub btnCalcDaysTaken_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCalcDaysTaken.Click
Try

DateTime(beginDate = dtpFrom.SelectedDate)
DateTime(endDate = dtpTo.SelectedDate)
dateDifference = beginDate.Subtract(endDate)

Catch ex As Exception

MessageBox.Show(ex.Message)

End Try
End Sub

It says datetime is a type and cannot be used as an expression.
Prasad_Kulkarni 11-Jul-12 1:58am    
You need to specify datatype for datetime
use TimeSpan dateDifference = beginDate.Subtract(endDate)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900