Search - Articles
DevASP.NET for ASP.NET, VB.NET, XML and C# (C-Sharp) Developers Tuesday, March 03, 2009
Dev Articles
Search Directory
ASP.NET
VB.Net
C-Sharp
SQL Server
 

Date Time Operations, How to subtract specified duration of time from date time object, getting days of specific month and comparing two objects of date time class using “Date Time” Class in C# Part5

Author: Zunnair
Download Source Code : 878_source.zip

In this article you will learn that how we can subtract specified duration of time from date time object, getting days of specific month and comparing two objects of Date Time class.

 

In my previous articles I discussed how to get time and date from system and also their components individually. Those articles will help you to understand this article more.
Days of Month:
Date Time class provides us a facility to get the number of days in the specific month of the year. For this purpose Date Time class provides us a non static function. This function takes two parameters, both are integer one is for month and other is for year, this function returns days as a integer, this function takes year as a parameter because if the year is leap and you are getting the number of days in February than it will vary from leap year to non leap years.
To demonstrate make a window application. Drag one text box and one button on the form. Press button you will get the result in text box.
Now write the following code on button click event
C#
private void btn_days_Click(object sender, EventArgs e)
        {
            DateTime obj = new DateTime();
            int d = 0;
            obj = DateTime.Now;
            d = DateTime.DaysInMonth(obj.Year, obj.Month);
            string str;
            str = d.ToString();
            txt_Result3.Text = str;
        }      
VB
Private Sub btn_days_Click(ByVal sender As Object, ByVal e As EventArgs)
        Dim obj As New DateTime()
        Dim d As Integer = 0
        obj = DateTime.Now
        d = DateTime.DaysInMonth(obj.Year, obj.Month)
        Dim str As String
        str = d.ToString()
        txt_Result3.Text = str
    End Sub
 This simple code tells you the number of days in a specific month.
 
Subtract Date Time Objects:
Date Time class also provides us a facility to subtract a specified duration of time from Date Time class object. For this purpose Date Time class provides us a function named subtract (). We can also subtract a Date time Object from other Date time object because Date Time class provides us one over loaded method.
First Method:
In first method this function takes object of time span class, which has specified time duration in it, as parameter and returns object of Date Time class which stores the time after subtraction.
To demonstrate make a window application. Drag one text box and one button on the form. Press button you will get the result in text box.
Now write the following code on button click event
C#
private void btn_Subtract_Click(object sender, EventArgs e)
        {
            DateTime obj = new DateTime();
            obj = DateTime.Now;
            txt_Result1.Text = obj.ToString();
            TimeSpan obj1 = new TimeSpan();
            obj1=TimeSpan.Zero;
            DateTime obj2 = new DateTime();            
            obj2 = obj.Subtract(obj1);
            string str = obj2.ToString();
            txt_Result_after.Text = str;
        }
VB
Private Sub btn_Subtract_Click(ByVal sender As Object, ByVal e As EventArgs)
        Dim obj As New DateTime()
        obj = DateTime.Now
        txt_Result1.Text = obj.ToString()
        Dim obj1 As New TimeSpan()
        obj1 = TimeSpan.Zero
        Dim obj2 As New DateTime()
        obj2 = obj.Subtract(obj1)
        Dim str As String = obj2.ToString()
        txt_Result_after.Text = str
    End Sub
Second Method:
In overloaded method this function takes object of Date Time class, and returns the object of Time Span class which stores the duration or difference between the times.
To demonstrate make a window application. Drag one text box and one button on the form. Press button you will get the result in text box.
Now write the following code on button click event
C#
private void btn_Subtract_Click(object sender, EventArgs e)
        {
            DateTime obj = new DateTime();
            obj = DateTime.Now;
            txt_Result1.Text = obj.ToString();
            DateTime obj1 = new DateTime();
            obj1 = DateTime.Today;
            TimeSpan obj2 = new TimeSpan();
            obj2 = obj.Subtract(obj1);
            string str = obj2.ToString();
            txt_Result_after.Text = str;
        }
VB
Private Sub btn_Subtract_Click(ByVal sender As Object, ByVal e As EventArgs)
        Dim obj As New DateTime()
        obj = DateTime.Now
        txt_Result1.Text = obj.ToString()
        Dim obj1 As New DateTime()
        obj1 = DateTime.Today
        Dim obj2 As New TimeSpan()
        obj2 = obj.Subtract(obj1)
        Dim str As String = obj2.ToString()
        txt_Result_after.Text = str
    End Sub
These simple codes tell that how we can subtract specific time duration from date time class object.
Compare:
Date Time provides us a facility that we can compare two object of Date Time class or we can compare two times. For this purpose compare class provides us a function named Compare (). This function takes two parameters both are objects of Date time class. This is a static function. This function returns the indication of relative values of objects as an integer.
To demonstrate make a window application. Drag one text box and one button on the form. Press button you will get the result in text box.
Now write the following code on button click event
C#
private void btn_Compare_Click(object sender, EventArgs e)
        {
            DateTime obj = new DateTime();
            DateTime obj1 = new DateTime();
            obj = DateTime.Now;
            obj1 = DateTime.Today;
            int d = 0;
            d = DateTime.Compare(obj, obj1);
            string str;
            str = d.ToString();
            txt_Result2.Text = str;
        }
VB
Private Sub btn_Compare_Click(ByVal sender As Object, ByVal e As EventArgs)
        Dim obj As New DateTime()
        Dim obj1 As New DateTime()
        obj = DateTime.Now
        obj1 = DateTime.Today
        Dim d As Integer = 0
        d = DateTime.Compare(obj, obj1)
        Dim str As String
        str = d.ToString()
        txt_Result2.Text = str
    End Sub
This simple code compares two object of Date Time class.
Now write the following code on FORM LOAD event:
C#
private void Form1_Load(object sender, EventArgs e)
        {
 
            this.Text = "Devasp.Net Application";
        }
VB
Private Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs)
 
        Me.Text = "Devasp.Net Application"
 End Sub
 
This simple article tells how we can subtract specified duration of time from date time object, getting days of specific month and comparing two objects of date time class using “Date Time” Class” in C# and VB.
Article Comments
Actually,good post. thx

Posted on 1/11/2010 10:00:30 PM by QFDaniel

   
Add Article Comment:
Name :
Email Address :
   
Comments :
 
   
<< Date Time Operations, How to check that year is leap or not, is time is in range of day light saving time or not and getting date as a day of weak and year using “Date Time” ” Class” in C# Part4

Disclaimer - Privacy
© 2002-2017 DevASP.net