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 (string, binary, local time, file time, file UTC time, Long Date String ) using “Date Time” ” Class” in C# (.net 2.0) Part 1.

Author: Zunnair
Download Source Code : 879_source.zip

In this article you will learn that how we can convert Date and Time in different formats (string, binary, local time, file time, file UTC time, Long Date String ) using “Date Time” Class in C#,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.
Date Time to String:
As we know that all the classes are inherited by object class and object class provides us some default functions which you will found in every class of C#. To convert the Date and Time in string, this is stored in object of Date and Time Class, we use function To String (). This function is provided by Date and Time class. This function takes nothing and returns string. This is called by the object of class so it converts the values of object 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_string_Click(object sender, EventArgs e)
        {
            DateTime obj = new DateTime();
            obj = DateTime.Now;
            string str;
            str = obj.ToString();
            txt_Result.Text = str;
        }
VB
Private Sub btn_string_Click(ByVal sender As Object, ByVal e As EventArgs)
        Dim obj As New DateTime()
        obj = DateTime.Now
        Dim str As String
        str = obj.ToString()
        txt_Result.Text = str
    End Sub
 This simple code converts the object of date time class into string.
Date Time to Binary:
Date Time class also provides us a facility to convert the Date Time class object into 64 bit binary. After that this binary can be use to make Date and Time. For this purpose date time class provides us function named To Binary (). This function takes nothing but it returns a long value after converting the object into binary through which it is called.
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_Binary_Click(object sender, EventArgs e)
        {
            DateTime obj = new DateTime();
            obj = DateTime.Now;
            string str;
            long l = 0;
            l = obj.ToBinary();
            str = l.ToString();
            txt_Result1.Text = str;
        }
VB
Private Sub btn_Binary_Click(ByVal sender As Object, ByVal e As EventArgs)
        Dim obj As New DateTime()
        obj = DateTime.Now
        Dim str As String
        Dim l As Long = 0
        l = obj.ToBinary()
        str = l.ToString()
        txt_Result1.Text = str
    End Sub
This simple code converts the object of date time class into binary.
Date Time to File Time:
Date Time class also provides us a facility to convert the Date Time class object into windows file time format. For this purpose date time class provides us function named To File Time (). This function takes nothing but it returns a long value after converting the object into window file time format through which it is called.
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_FileTime_Click(object sender, EventArgs e)
        {
            DateTime obj = new DateTime();
            obj = DateTime.Now;
            string str;
            long l = 0;
            l = obj.ToFileTime();
            str = l.ToString();
            txt_Result2.Text = str;
        }
VB
Private Sub btn_FileTime_Click(ByVal sender As Object, ByVal e As EventArgs)
        Dim obj As New DateTime()
        obj = DateTime.Now
        Dim str As String
        Dim l As Long = 0
        l = obj.ToFileTime()
        str = l.ToString()
        txt_Result2.Text = str
    End Sub
 
This simple code converts the object of date time class into windows file time format.
Date Time to File Time (Coordinate Universal Time):
Date Time class also provides us a facility to convert the Date Time class object into windows file time with coordinate universal time format. For this purpose date time class provides us function named To File Time UTC (). This function takes nothing but it returns a long value after converting the object into window file time with coordinate universal time format through which it is called.
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_FileTimeUTC_Click(object sender, EventArgs e)
        {
            DateTime obj = new DateTime();
            obj = DateTime.Now;
            string str;
            long l = 0;
            l = obj.ToFileTimeUtc();
            str = l.ToString();
            txt_Result2.Text = str;
        }
VB
Private Sub btn_FileTimeUTC_Click(ByVal sender As Object, ByVal e As EventArgs)
        Dim obj As New DateTime()
        obj = DateTime.Now
        Dim str As String
        Dim l As Long = 0
        l = obj.ToFileTimeUtc()
        str = l.ToString()
        txt_Result2.Text = str
    End Sub
 
This simple code converts the object of date time class into windows file time with coordinate universal time format.
Date Time to Long 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 long string date. For this purpose date time class provides us function named to long date string (). This function takes nothing but it returns a string value after converting the object into long date 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_LongDate_Click(object sender, EventArgs e)
        {
            DateTime obj = new DateTime();
            obj = DateTime.Now;
            string str;
            str = obj.ToLongDateString();
            txt_Result5.Text = str;
        }
VB
Private Sub btn_LongDate_Click(ByVal sender As Object, ByVal e As EventArgs)
        Dim obj As New DateTime()
        obj = DateTime.Now
        Dim str As String
        str = obj.ToLongDateString()
        txt_Result5.Text = str
    End Sub
This simple code converts the object of date time class into long string date.
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 (string, binary, local time, file time, file UTC time, Long Date String ) using “Date Time” Class” in C# and VB.
Article Comments
I was really looking for the case of converting a WIN32.FILETIME to a .NET DateTime or at least to derive the date time information from the filetime.

Thanks in advance

Posted on 4/23/2010 11:11:17 AM by JohnA

   
Add Article Comment:
Name :
Email Address :
   
Comments :
 
   
<< 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

Disclaimer - Privacy
© 2002-2017 DevASP.net