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 add values or change values of components of date and time (day, month, year, minute, hour, second, millisecond) using “Date Time” ” Class” in C# (.net 2.0) Part3.

Author: Zunnair
Download Source Code : 875_source.zip

In this article you will learn that how we can add values or change values of components of date and time (day, month, year, minute, hour, second, millisecond) using “Date Time” Class” in C# and VB.

 

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.
To change the components of the date and time, date time class provides us different functions.
Add Day:
To add values in day first you will have to get the date after that you have to get day component out of date. After getting you can add values in day, for this purpose “Date and Time” class provides us a function named Add Day (). This function takes double value which you want to add in current date and returns object of Date and Time class, object has the new values after modification.
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_Day_Click(object sender, EventArgs e)
        {
            DateTime obj = new DateTime();
            obj = DateTime.Now;
            double d=10;
            obj=obj.AddDays(d);
            string str = obj.ToString();
            txt_Result1.Text = str;
        }
VB
Private Sub btn_Day_Click(ByVal sender As Object, ByVal e As EventArgs)
        Dim obj As New DateTime()
        obj = DateTime.Now
        Dim d As Double = 10
 obj = obj.AddDays(d)
       Dim str As String = obj.ToString()
        txt_Result1.Text = str
    End Sub
This simple code adds value in the DAY component.
 
ADD Month:
To add values in month first you will have to get the date after that you have to get month component out of date. After getting you can add values in month, for this purpose “Date and Time” class provides us a function named Add Month (). This function takes integer value which you want to add in current date and returns object of Date and Time class, object has the new values after modification.
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_Month_Click(object sender, EventArgs e)
        {
            DateTime obj = new DateTime();
            obj = DateTime.Now;
            int d = 10;
            obj = obj.AddMonths(d);
            string str = obj.ToString();
            txt_Result2.Text = str;
        }
VB
Private Sub btn_Month_Click(ByVal sender As Object, ByVal e As EventArgs)
        Dim obj As New DateTime()
        obj = DateTime.Now
        Dim d As Integer = 10
       obj = obj.AddMonths(d)
        Dim str As String = obj.ToString()
        txt_Result2.Text = str
    End Sub
This simple code adds value in the MONTH component.
ADD Year:
To add values in year first you will have to get the date after that you have to get year component out of date. After getting you can add values in year, for this purpose “Date and Time” class provides us a function named Add Year (). This function takes integer value which you want to add in current date and returns object of Date and Time class, object has the new values after modification.
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_year_Click(object sender, EventArgs e)
        {
            DateTime obj = new DateTime();
            obj = DateTime.Now;
            int d = 10;
            obj = obj.AddYears(d);
            string str = obj.ToString();
            txt_Result3.Text = str;
        }
VB
Private Sub btn_year_Click(ByVal sender As Object, ByVal e As EventArgs)
        Dim obj As New DateTime()
        obj = DateTime.Now
        Dim d As Integer = 10
       obj = obj.AddYears(d)
        Dim str As String = obj.ToString()
        txt_Result3.Text = str
    End Sub
This simple code adds value in the YEAR component.
ADD Hour:
To add values in hour first you will have to get the time after that you have to get hour component out of time. After getting you can add values in hour, for this purpose “Date and Time” class provides us a function named Add Hour (). This function takes double value which you want to add in current date and returns object of Date and Time class, object has the new values after modification.
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_hour_Click(object sender, EventArgs e)
        {
            DateTime obj = new DateTime();
            obj = DateTime.Now;
            double d = 10;
            obj = obj.AddHours(d);
            string str = obj.ToString();
            txt_Result4.Text = str;
        }
VB
Private Sub btn_hour_Click(ByVal sender As Object, ByVal e As EventArgs)
        Dim obj As New DateTime()
        obj = DateTime.Now
        Dim d As Double = 10
 obj = obj.AddHours(d)
       Dim str As String = obj.ToString()
        txt_Result4.Text = str
    End Sub
This simple code adds value in the HOUR component.
ADD Minute:
To add values in minute first you will have to get the time after that you have to get minute component out of time. After getting you can add values in minute, for this purpose “Date and Time” class provides us a function named Add Minute (). This function takes double value which you want to add in current date and returns object of Date and Time class, object has the new values after modification.
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_minute_Click(object sender, EventArgs e)
        {
            DateTime obj = new DateTime();
            obj = DateTime.Now;
            double d = 10;
            obj = obj.AddMinutes(d);
            string str = obj.ToString();
            txt_Result5.Text = str;
        }
VB
Private Sub btn_minute_Click(ByVal sender As Object, ByVal e As EventArgs)
        Dim obj As New DateTime()
        obj = DateTime.Now
        Dim d As Double = 10
 obj = obj.AddMinutes(d)
        Dim str As String = obj.ToString()
        txt_Result5.Text = str
    End Sub
This simple code adds value in the MINUTE component.
ADD Second:
To add values in seconds first you will have to get the time after that you have to get seconds component out of time. After getting you can add values in second, for this purpose “Date and Time” class provides us a function named Add second (). This function takes double value which you want to add in current date and returns object of Date and Time class, object has the new values after modification.
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_second_Click(object sender, EventArgs e)
        {
            DateTime obj = new DateTime();
            obj = DateTime.Now;
            double d=10;
            obj=obj.AddSeconds(d);
            string str = obj.ToString();
            txt_Result6.Text = str;
        }
 VB
Private Sub btn_second_Click(ByVal sender As Object, ByVal e As EventArgs)
        Dim obj As New DateTime()
        obj = DateTime.Now
        Dim d As Double = 10
 obj = obj.AddSeconds(d)
        Dim str As String = obj.ToString()
        txt_Result6.Text = str
    End Sub
This simple code adds value in the SECOND component.
ADD Milliseconds:
To add values in milliseconds first you will have to get the time after that you have to get milliseconds component out of time. After getting you can add values in millisecond, for this purpose “Date and Time” class provides us a function named Add Millisecond (). This function takes double value which you want to add in current date and returns object of Date and Time class, object has the new values after modification.
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.
C#
private void button1_Click(object sender, EventArgs e)
        {
            DateTime obj = new DateTime();
            obj = DateTime.Now;
            double d = 10;
            obj = obj.AddMilliseconds(d);
            string str = obj.ToString();
            txt_Result7.Text = str;
        }
VB
Private Sub button1_Click(ByVal sender As Object, ByVal e As EventArgs)
        Dim obj As New DateTime()
        obj = DateTime.Now
        Dim d As Double = 10
 obj = obj.AddMilliseconds(d)
        Dim str As String = obj.ToString()
        txt_Result7.Text = str
    End Sub
This simple code adds value in the MILLISECOND component.
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 add values or change values of components of date and time (day, month, year, minute, hour, second, millisecond) using “Date Time” Class” in C# and VB.
 
Article Comments
how can i add two dates which is displayed in different textboxs

Posted on 7/19/2010 7:47:25 AM by thilaga

   
Add Article Comment:
Name :
Email Address :
   
Comments :
 
   
<< Date Time Operations, How to get the components of date and time (day, month, year, minute, hour, second, millisecond) using “Date Time” ” Class” in C# (.net 2.0) Part2.

Disclaimer - Privacy
© 2002-2017 DevASP.net