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 Conversion in different formats (long time sting, OA date, short date string, short date string, universal time) using “Date Time” ” Class” in C# (.net 2.0) Part 2.

Author: Zunnair
Download Source Code : 880_source.zip

In this article you will learn that how we can convert Date and Time in different formats (long time sting, OA date, short date string, short date string, universal time) using“Date Time” Class in C#.

 

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.
In the first part of this article I discussed some other formats and conversion of Date Time class object.
Date Time to Long Time String:
The object of Date Time class has both date and time simultaneously so Date Time class also provides us a facility to convert the Date Time class object only in long string time. For this purpose date time class provides us function named to long time string (). This function takes nothing but it returns a string value after converting the object into long time string through which it is called. It separates the time and converts it into string.
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_longtime_Click(object sender, EventArgs e)
        {
            DateTime obj = new DateTime();
            obj = DateTime.Now;
            string str;
            str = obj.ToLongTimeString();
            txt_Result.Text = str;
        }
VB
Private Sub btn_longtime_Click(ByVal sender As Object, ByVal e As EventArgs)
        Dim obj As New DateTime()
        obj = DateTime.Now
        Dim str As String
        str = obj.ToLongTimeString()
        txt_Result.Text = str
    End Sub
 
This simple code converts the object of date time class into long string time.
Date Time to OLE Automation Date:
Date Time class also provides us a facility to convert the Date Time class object in to ole automation date. For this purpose date time class provides us function named To OA (). This function takes nothing but it returns a double value after converting the object into Ole Automation date through which it is called. This function separates the date and than converts it.
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_OA_Click(object sender, EventArgs e)
        {
            DateTime obj = new DateTime();
            obj = DateTime.Now;
            string str;
            double d = 0;
            d = obj.ToOADate();
            str = d.ToString();
            txt_Result1.Text = str;
        }
VB
Private Sub btn_OA_Click(ByVal sender As Object, ByVal e As EventArgs)
        Dim obj As New DateTime()
        obj = DateTime.Now
        Dim str As String
        Dim d As Double = 0
        d = obj.ToOADate()
        str = d.ToString()
        txt_Result1.Text = str
    End Sub
This simple code converts the object of date time class into Ole automation date.
Date Time to Short string date:
The object of Date Time class has both date and time simultaneously so Date Time class also provides us a facility to convert the Date Time class object only in short string date. For this purpose date time class provides us function named to short time string (). This function takes nothing but it returns a string value after converting the object into short time string through which it is called. It separates the date and converts it into string.
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_shortdate_Click(object sender, EventArgs e)
        {
            DateTime obj = new DateTime();
            obj = DateTime.Now;
            string str;
            str = obj.ToShortDateString();
            txt_Result2.Text = str;
        }
VB
Private Sub btn_shortdate_Click(ByVal sender As Object, ByVal e As EventArgs)
        Dim obj As New DateTime()
        obj = DateTime.Now
        Dim str As String
        str = obj.ToShortDateString()
        txt_Result2.Text = str
    End Sub
This simple code converts the object of date time class into short string date.
Date Time to Short string time:
The object of Date Time class has both date and time simultaneously so Date Time class also provides us a facility to convert the Date Time class object only in short string time. For this purpose date time class provides us function named to short time string (). This function takes nothing but it returns a string value after converting the object into short time string through which it is called. It separates the time and converts it into string.
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_shorttime_Click(object sender, EventArgs e)
        {
            DateTime obj = new DateTime();
            obj = DateTime.Now;
            string str;
            str = obj.ToShortTimeString();
            txt_Result3.Text = str;
        }
VB
Private Sub btn_shorttime_Click(ByVal sender As Object, ByVal e As EventArgs)
        Dim obj As New DateTime()
        obj = DateTime.Now
        Dim str As String
        str = obj.ToShortTimeString()
        txt_Result3.Text = str
    End Sub
 
This simple code converts the object of date time class into short string time.
Date Time to Universal Time Format:
Date Time class also provides us a facility to convert the Date Time class object into universal time format. For this purpose date time class provides us function named to Universal Time (). This function takes nothing but it returns an object of date time class which has the universal time format.
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_longtime_Click(object sender, EventArgs e)
        {
            DateTime obj = new DateTime();
            obj = DateTime.Now;
            string str;
            str = obj.ToLongTimeString();
            txt_Result.Text = str;
        }
VB
Private Sub btn_longtime_Click(ByVal sender As Object, ByVal e As EventArgs)
        Dim obj As New DateTime()
        obj = DateTime.Now
        Dim str As String
        str = obj.ToLongTimeString()
        txt_Result.Text = str
    End Sub
 
This simple code converts the object of date time class into universal time format.
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 convert Date and Time in different formats (long time sting, OA date, short date string, short date string, universal time) using “Date Time” Class” in C# and VB.
   
Add Article Comment:
Name :
Email Address :
   
Comments :
 
   
<< Date Time Conversion in different formats (string, binary, local time, file time, file UTC time, Long Date String ) using “Date Time” ” Class” in C# (.net 2.0) Part 1.

Disclaimer - Privacy
© 2002-2017 DevASP.net