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 Date and Time (Now, Today, Date, Time of Day, Co-ordinate universal time) using “Date Time” Class” in C# (.net 2.0) Part1.

Author: Zunnair
Download Source Code : 873_Date time.zip

In this article you will learn that how we can get Date and Time (Now, Today, Date, Time of Day, Co-ordinate Universal time) using “Date Time” Class” in C# and VB.

 

Dot net provides us a class named to “Date Time”. This class helps us to do different operations with date and time. There are different functions and properties which deal with the date and time.
Date and Time class provides us static and non static functions. As we know that static can be called without object or with the name of class and non static functions can only be called by object.
Get Date and Time:
First of all we learn that how we can get a date and time. This task can perform by both static and non static functions.
Get Date:
To get date “Date and Time” class provides us a property named Date. This property returns us a date time class object. Object has time and date both to separate the date we can use function ToShortDateString();
To demonstrate make a window application. Drag one text box and one button on the form. Press button you will get the date in text box.
Now write the following code on button click event
C#
private void btn_Date_Click(object sender, EventArgs e)
        {
            DateTime obj = new DateTime();
            obj=obj.Date;
            string str = obj.ToShortDateString();
            txt_Result1.Text = str;
        }
VB
Private Sub btn_Date_Click(ByVal sender As Object, ByVal e As EventArgs)
        Dim obj As New DateTime()
        obj = obj.[Date]
        Dim str As String = obj.ToShortDateString()
        txt_Result1.Text = str
    End Sub
This simple code gets the date.
Get Time:
To get time Date and Time class provides us a property named Time of Day. This property returns the object of a time span class. Object of time span has date and time both in it so we can separate time by using the function named ToShortTimeString();
To demonstrate make a window application. Drag one text box and one button on the form. Press button you will get the time in text box.
Now write the following code on button click event
C#
private void btn_time_Click(object sender, EventArgs e)
        {
           TimeSpan obj = new TimeSpan();
            DateTime obj1 = new DateTime();
            obj = obj1.TimeOfDay;
            string str = obj1.ToShortTimeString();
            txt_Result2.Text=str;
        }
VB
Private Sub btn_time_Click(ByVal sender As Object, ByVal e As EventArgs)
        Dim obj As New TimeSpan()
        Dim obj1 As New DateTime()
        obj = obj1.TimeOfDay
        Dim str As String = obj1.ToShortTimeString()
        txt_Result2.Text = str
    End Sub
This simple code gets the time.
Get Today’s Date and Time Simultaneously:
To get today’s time and date simultaneously; Date and Time class provides us a property named today. This property returns the object of a date and time class. Object has the values of both date and time. To get this date and time we use static property. You can also separate time and date by using functions as I told above.
To demonstrate make a window application. Drag one text box and one button on the form. Press button you will get the date and time in text box.
Now write the following code on button click event
C#
private void btn_today_Click(object sender, EventArgs e)
        {
            DateTime obj=new DateTime();
            obj=DateTime.Today;
            string str=obj.ToString();
            txt_Result3.Text=str;
        }
VB
Private Sub btn_today_Click(ByVal sender As Object, ByVal e As EventArgs)
        Dim obj As New DateTime()
        obj = DateTime.Today
        Dim str As String = obj.ToString()
        txt_Result3.Text = str
    End Sub
This simple code gets today’s time and date simultaneously.
Get Current Date and Time Simultaneously:
To get current time and date simultaneously; Date and Time class provides us a property named now. This property returns the object of a date and time class. Object has the values of both date and time. To get this date and time we use static property. You can also separate time and date by using functions as I told above. This property takes time from your system.
To demonstrate make a window application. Drag one text box and one button on the form. Press button you will get the date and time in text box.
Now write the following code on button click event
C#
private void btn_now_Click(object sender, EventArgs e)
        {
            DateTime obj = new DateTime();
            obj = DateTime.Now;
            string str = obj.ToString();
            txt_Result4.Text = str;
        }
VB
Private Sub btn_now_Click(ByVal sender As Object, ByVal e As EventArgs)
        Dim obj As New DateTime()
        obj = DateTime.Now
        Dim str As String = obj.ToString()
        txt_Result4.Text = str
    End Sub
This simple code gets the current date and time of your system.
Get Co-Ordinate Universal Time:
To get co-ordinate universal time Date and Time class provides us a property named “UTF Now”. This property returns the object of a date and time class. Object has the values of both date and time. To get this date and time we use static property. You can also separate time and date by using functions as I told above. This property takes time from your system and converts that time it co ordinate universal time.
To demonstrate make a window application. Drag one text box and one button on the form. Press button you will get the date and time in text box.
Now write the following code on button click event
C#
private void btn_utf_Click(object sender, EventArgs e)
        {
            DateTime obj = new DateTime();
            obj = DateTime.UtcNow;
            string str = obj.ToString();
            txt_Result5.Text = str;
        }
VB
Private Sub btn_utf_Click(ByVal sender As Object, ByVal e As EventArgs)
        Dim obj As New DateTime()
        obj = DateTime.UtcNow
        Dim str As String = obj.ToString()
        txt_Result5.Text = str
    End Sub
This simple code gets the system time and converts it in co ordinate universal 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 Date and Time (Now, Today, Date, Time of Day, Co-ordinate Universal time) using “Date Time” Class” in C# and VB.
Article Comments
it is good article to us.

Posted on 12/10/2009 4:40:18 AM by sankar

hey..tnx..alot..for this tutorial about date and time..it is a big help for me..

Posted on 2/7/2010 7:31:28 PM by george

THANK U FOR THE VERY SIMPLE CODE

Posted on 3/8/2010 12:38:01 PM by NOBLE.C.J

hi,
thanx alot for give date and time it's give me lot of help in my project

thanx ,thanx ,thanx ........................................

Posted on 8/13/2010 1:25:36 AM by Rajan Dubey

hi,
thanx alot for give date and time it's give me lot of help in my project

thanx ,thanx ,thanx ........................................

Posted on 8/13/2010 1:26:43 AM by Rajan Dubey

Hey Nice one.. was of good use for me..

thanx..

VIN..

Posted on 8/26/2010 5:01:02 AM by VIN

Also the following code works..

txt_txtBx.Text=System.DateTime.Now.ToString();

Posted on 8/26/2010 5:04:34 AM by VIN

   
Add Article Comment:
Name :
Email Address :
   
Comments :
 
   
<< How to get special folders path (cookies, templates, history etc…….) using “Environment Class” in C# (.net 2.0).

Disclaimer - Privacy
© 2002-2017 DevASP.net