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
 

Working with Date Time & Time Span types in Asp.net.

Author: Zunnair

In this article I will try to show the way in which you can use DateTime & TimeSpan types in useful and Exact manner for for which it is made.

This Article is Useful because it Allows you to interactively work with most of the properties and methods of the DateTime and TimeSpan classes.

 

First of all add region in the load event of your Form.

has been added to some procedures since they are not the focus of the demo. Remove them if you wish to debug the procedures.So that we only need to set the title of the application once,we use the AssemblyInfo class (defined in the AssemblyInfo.vb file) to read the AssemblyTitle attribute.

Call procedures that load values onto the form.

 

VB.Net

 

#Region " Form Load "

            Private Sub frmMain_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load

                        Dim ainfo As New AssemblyInfo()

 

                        Me.Text = ainfo.Title

                        Me.mnuAbout.Text = String.Format("&About {0} ...", ainfo.Title)

 

                                                LoadSharedMembers()

                        LoadProperties()

                        LoadConversionMethods()

                        LoadCalculationMethods()

                        LoadTimeSpanFields()

 

                        LoadTSMethods()

            End Sub

#End Region

 

  1. C#.NET

#region " Form Load "

  1. [System.Diagnostics.DebuggerStepThrough()]
  2. private void frmMain_Load(object sender, System.EventArgs e)
  3. {
  4.     AssemblyInfo ainfo = new AssemblyInfo();
  5.     this.Text = ainfo.Title;
  6.     this.mnuAbout.Text = string.Format("&About {0} ...", ainfo.Title);
  7.     LoadSharedMembers();
  8.     LoadProperties();
  9.     LoadConversionMethods();
  10.     LoadCalculationMethods();
  11.     LoadTimeSpanFields();
  12.     LoadTSMethods();
  13. }
  14. #endregion

 

Now write tye routine below. Remember No need to use an explicit constructor unless you need to specify a value at the time the instance gets created. it also Display the properties of the TimeSpan instance you've created.

 

Private Sub btnCalcParse_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCalcParse.Click

                        Try

                                    Dim ts As TimeSpan

                                    ts = TimeSpan.Parse(txtParse.Text)

 

                                    DisplayTSProperties(ts)

                        Catch exp As Exception

                                    MessageBox.Show(exp.Message, Me.Text)

                        End Try

            End Sub

  1. #region " Form Load "
  2. [System.Diagnostics.DebuggerStepThrough()]
  3. private void frmMain_Load(object sender, System.EventArgs e)
  4. {
  5.     AssemblyInfo ainfo = new AssemblyInfo();
  6.     this.Text = ainfo.Title;
  7.     this.mnuAbout.Text = string.Format("&About {0} ...", ainfo.Title);
  8.     LoadSharedMembers();
  9.     LoadProperties();
  10.     LoadConversionMethods();
  11.     LoadCalculationMethods();
  12.     LoadTimeSpanFields();
  13.     LoadTSMethods();
  14. }
  15. #endregion

 

 

This routine Create a TimeSpan instance based on DateTime values provided on the form. Parse the text from the text boxes.

Display the properties of the TimeSpan instance you've created.

Private Sub btnRefreshTSProperties_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRefreshTSProperties.Click

                        Try

                                    Dim ts As TimeSpan

                                    Dim dtStart As DateTime

                                    Dim dtEnd As DateTime

                                    dtStart = DateTime.Parse(txtStart.Text)

                                    dtEnd = DateTime.Parse(txtEnd.Text)

                                    ts = dtEnd.Subtract(dtStart).Duration

                                    DisplayTSProperties(ts)

                        Catch Exp As Exception

                                    MessageBox.Show(Exp.Message, Me.Text)

                        End Try

End Sub

 

Some demonstrations require you to supply values (the DateTime Shared Members page, for example). By default, the values in the text boxes are arbitrary values. Enter values into the appropriate text boxes, then click the page's Refresh button to run the corresponding method and refill the Label controls displaying the results. The code pulls its parameter value(s) from the text box(es). See the code to determine how the various methods use the parameters supplied by the text boxes. The TimeSpan Properties tab allows you to calculate TimeSpan values in two ways: either specifying a range of values, or by parsing a String containing a representation for a TimeSpan value. Select the tab you want to try, enter values, and then click the Calculate button. 

 

This is a simple article .i hope u will like and enjoying this Article. If you feel any difficulty then reference the source code.because Source code is given with this Article for your proper understanding.

 

Article Comments
great experience, dude! thanks for this great

Articles wow... it's very wonderful report.

Posted on 8/2/2010 9:30:01 PM by mbt shoes

Very good sharing this.

Posted on 8/4/2010 2:09:19 PM by free registry cleaner

Great sharing this.

Posted on 8/10/2010 10:54:10 AM by make money easy

A good article Thank you!

Posted on 8/10/2010 2:49:54 PM by business finance software

AssemblyInfo ainfo = new AssemblyInfo();
this.Text = ainfo.Title;
this.mnuAbout.Text = string.Format("&About {0} ...", ainfo.Title);
LoadSharedMembers();
LoadProperties();
LoadConversionMethods();
LoadCalculationMethods();
LoadTimeSpanFields();
LoadTSMethods();

Posted on 11/4/2010 2:29:05 AM by gg

   
Add Article Comment:
Name :
Email Address :
   
Comments :
 
   
<< Save and read file from server in Asp.Net using .Net 2.0.

Disclaimer - Privacy
© 2002-2017 DevASP.net