This article will show you how you can get the no of date between two dates by using timespan in
windows application using c#.net and VB.net. In this I have used dateTimePicker.
Some of my previous articles are as follows: Validate
Directory Exist or Not in C#.Net In Windows Application, Image
Save In XML and Load XML Bitmap Image File in Windows Application Using C#.Net,
How
to Save Record in XML File and Read XML to Display in DataGridview Using C#.net
in Windows Application, How
to Add HyperLink and Retrieve Row Value on Link Click in DataGridView #,
Windows application, How
to Add Button Control and Retrieve Row Value on Button Click in DataGridView
Using C#.Net, Windows Application, Splash
Screen With Please Wait OR Loading Message in C#.net in Windows Application,
Save
Panel with Control Inside It as Image C#.net, VB.net in windows application.
So for this article first we will create a new windows
application and add the dateTimePicker controls and button control in form. Now
on button click event add the below code as shown below.
C#.Net
using System;
using System.Windows.Forms;
using System.IO;
namespace WindowsFormsApplication9
{
public partial class Form1 : Form
{
public
Form1()
{
InitializeComponent();
}
private
void button1_Click(object
sender, EventArgs e)
{
DateTime
startDate = Convert.ToDateTime(dateTimePicker1.Value);
DateTime
EndDate = Convert.ToDateTime(dateTimePicker2.Value);
/*Get
time span between two days*/
TimeSpan
dateDiff = EndDate - startDate;
/*Get
no of days*/
int
diffInDays = dateDiff.Days;
MessageBox.Show("Total no of days between tow days : "
+ diffInDays.ToString());
}
}
}
|
VB.Net
Imports System.Windows.Forms
Imports System.IO
Namespace WindowsFormsApplication9
Partial Public Class Form1
Inherits
Form
Public
Sub New()
InitializeComponent()
End Sub
Private
Sub button1_Click(ByVal
sender As Object,
ByVal e As
EventArgs)
Dim
startDate As DateTime =
Convert.ToDateTime(dateTimePicker1.Value)
Dim
EndDate As DateTime =
Convert.ToDateTime(dateTimePicker2.Value)
'Get time span
between two days
Dim
dateDiff As TimeSpan = EndDate - startDate
'Get no
of days
Dim
diffInDays As Integer
= dateDiff.Days
MessageBox.Show("Total no of days between tow days : "
+ diffInDays.ToString())
End Sub
End Class
End Namespace
|
In above code I have stored both control date into variable
of datetime datatype. After that I have find the timespan and get the no of
days .
Now we have done run the application and check the output.
0 comments:
Please let me know your view