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

Author: Zunnair
Download Source Code : 874_date time components.zip

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

 

In my previous article I discussed how to get time and date. That article will help you to understand this article more.
To get the components of the date and time, date time class provides us different properties.
Get Day:
To get day out of date first you will have to get the date after that you can get the day component out of that for this purpose “Date and Time” class provides us a property named Day. This property returns the value of day 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_Day_Click(object sender, EventArgs e)
        {
            DateTime obj = new DateTime();
            obj = DateTime.Now;
            int a = obj.Day;
            string str = a.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 a As Integer = obj.Day
        Dim str As String = a.ToString()
        txt_Result1.Text = str
    End Sub
This simple code gets the day component out of the date.
 
Get Month:
To get month out of date first you will have to get the date after that you can get the month component out of that for this purpose “Date and Time” class provides us a property named Month. This property returns the value of month 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_Month_Click(object sender, EventArgs e)
        {
            DateTime obj = new DateTime();
            obj = DateTime.Now;
            int a = obj.Month;
            string str = a.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 a As Integer = obj.Month
        Dim str As String = a.ToString()
        txt_Result2.Text = str
    End Sub
This simple code gets the month component out of the date.
Get Year:
To get year out of date first you will have to get the date after that you can get the year component out of that for this purpose “Date and Time” class provides us a property named Year. This property returns the value of year 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_year_Click(object sender, EventArgs e)
        {
            DateTime obj = new DateTime();
            obj = DateTime.Now;
            int a = obj.Year;
            string str = a.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 a As Integer = obj.Year
        Dim str As String = a.ToString()
        txt_Result3.Text = str
    End Sub
This simple code gets the year component out of the date
Get Hour:
To get hour out of time first you will have to get the time after that you can get the hour component out of that for this purpose “Date and Time” class provides us a property named Hour. This property returns the value of hours 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_hour_Click(object sender, EventArgs e)
        {
            DateTime obj = new DateTime();
            obj = DateTime.Now;
            int a = obj.Hour;
            string str = a.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 a As Integer = obj.Hour
        Dim str As String = a.ToString()
        txt_Result4.Text = str
    End Sub
This simple code gets the hour’s component out of the time.
Get Minute:
To get minute out of time first you will have to get the time after that you can get the minute component out of that for this purpose “Date and Time” class provides us a property named minute. This property returns the value of minutes 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_minute_Click(object sender, EventArgs e)
        {
            DateTime obj = new DateTime();
            obj = DateTime.Now;
            int a = obj.Minute;
            string str = a.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 a As Integer = obj.Minute
        Dim str As String = a.ToString()
        txt_Result5.Text = str
    End Sub
This simple code gets the minute’s component out of the time.
Get Second:
To get second out of time first you will have to get the time after that you can get the second’s component out of that for this purpose “Date and Time” class provides us a property named second. This property returns the value of seconds 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_second_Click(object sender, EventArgs e)
        {
            DateTime obj = new DateTime();
            obj = DateTime.Now;
            int a = obj.Second;
            string str = a.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 a As Integer = obj.Second
        Dim str As String = a.ToString()
        txt_Result6.Text = str
    End Sub
This simple code gets the second’s component out of the time.
Get Milliseconds:
To get millisecond out of time first you will have to get the time after that you can get the millisecond component out of that for this purpose “Date and Time” class provides us a property named millisecond. This property returns the value of milliseconds 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.
C#
private void button1_Click(object sender, EventArgs e)
        {
            DateTime obj = new DateTime();
            obj = DateTime.Now;
            int a = obj.Millisecond;
            string str = a.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 a As Integer = obj.Millisecond
        Dim str As String = a.ToString()
        txt_Result7.Text = str
    End Sub
This simple code gets the millisecond’s component out of the time.
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 get the components of date and time (day, month, year, minute, hour, second, millisecond) using “Date Time” Class” in C# and VB.
   
Add Article Comment:
Name :
Email Address :
   
Comments :
 
   
<< Date Time Operations. How to get the Date and Time (Now, Today, Date, Time of Day, Co-ordinate universal time) using “Date Time” Class” in C# (.net 2.0) Part1.

Disclaimer - Privacy
© 2002-2017 DevASP.net