HOME  |    TRAINING  |   FREE TUTORIALS   |   JOBS
Find out more about our new RSS feed.
FREE Tutorial
C# PROGRAMMING: NAMESPACES AND THE BASE CLASSES PART 3 - MANIPULATING DATES AND TIMES

CATEGORY
SEARCH OUR OTHER TUTORIALS

DESCRIPTION

Our first samples will demonstrate how to access the date-time functionality provided by the base classes. There are two classes of relevance here: DateTime and TimeSpan, both of which are in the System namespace.


This free tutorial is a sample from the book C# Programming with the Public Beta.


We start off by displaying the current date and time in various different formats, using this code:

namespace Wrox.SampleCode.CSharpPreview.ChBaseClasses
{
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.WinForms;
using System.Data;

public class FormDisplayDateTimes : System.WinForms.Form
{
... as before

/// <summary>
///  Summary description for FormDisplayDateTimes.
/// </summary>
public FormDisplayDateTimes()
{
  //
  // Required for Win Form Designer support
  //


  InitializeComponent();

  //
  // TODO: Add any constructor code after InitializeComponent call
  //
  DateTime dtCurrTime = DateTime.Now;
  AddItem("Current Time is " + dtCurrTime.ToString());
  AddItem("Year is " + dtCurrTime.Year.ToString());
  AddItem("Month is " + dtCurrTime.Month.ToString());
  AddItem("Day of month is " + dtCurrTime.Day.ToString());
  AddItem("Day of week is " + dtCurrTime.DayOfWeek.ToString());
  AddItem("Hour is " + dtCurrTime.Hour.ToString());
  AddItem("Minute is " + dtCurrTime.Minute.ToString());
  AddItem("Second is " + dtCurrTime.Second.ToString());
  AddItem("Millisecond is " + dtCurrTime.Millisecond.ToString());
  AddItem("ShortDateString is " + dtCurrTime.ToShortDateString());
  AddItem("LongDateString is " + dtCurrTime.ToLongDateString());
  AddItem("ShortTimeString is " + dtCurrTime.ToShortTimeString());
  AddItem("LongTimeString is " + dtCurrTime.ToLongTimeString());
}

As explained earlier, we are adding our code to the constructor of the FormDisplayDateTimes class in our project. (FormDisplayDateTimes was Form1 in the generated code but we've changed its name to a more meaningful class name.)

We first instantiate a variable dtCurrTime of class DateTime, and initialize it using the static property of the DateTime class, Now, which returns the current date and time. We then use various properties, fields and methods to extract portions of the current date and time. This code produces the following output:

Next we will create another sample, which we'll call the FormTimeSpans sample. This shows how to add and take the differences between date-times. This is where the TimeSpan class comes in. The TimeSpan class represents a difference between two date-times. The following sample takes a DateTime, initialized to 1 January 2000, 12 pm, adds an interval of 4 days 2 hours 15 minutes to it, and displays the results along with information about the times and timespan.

...

public class FormTimeSpans : System.WinForms.Form
{
... as before

public FormTimeSpans()
{
  //
  // Required for Win Form Designer support
  //
  InitializeComponent();

  //
  // TODO: Add any constructor code after InitializeComponent call
  //

  // constructor: TimeSpan(days, hours, minutes, seconds)
  TimeSpan Span = new TimeSpan(4,2,15,0);
  
  // initialize date to 1 Jan 2000, 12 pm
  // constructor: DateTime(year,month,day,hours,minutes,seconds, 
  // milliseconds)
  DateTime dtOld = new DateTime(2000,1,1,12,0,0,0);

  DateTime dtNew = dtOld + Span;

  AddItem("Original date was " + dtOld.ToLongDateString() + 
      " " + dtOld.ToShortTimeString());
  AddItem("Adding time span of " + Span.ToString());
  AddItem("Result is " + dtNew.ToLongDateString() + " " +
      dtNew.ToShortTimeString());
  AddItem("");
  AddItem("Time span broken down is:");
  AddItem("Days: " + Span.Days.ToString());
  AddItem("Hours: " + Span.Hours.ToString());
  AddItem("Minutes: " + Span.Minutes.ToString());
  AddItem("Seconds: " + Span.Seconds.ToString());
  AddItem("Milliseconds: " + Span.Milliseconds.ToString());
  AddItem("Ticks: " + Span.Ticks.ToString());
  AddItem("");
  AddItem("TicksPerSecond: " + TimeSpan.TicksPerSecond.ToString());
  AddItem("TicksPerHour: " + TimeSpan.TicksPerHour.ToString());

}

In this sample we see the new operator being used to construct DateTime and TimeSpan instances of given value. Both these classes have several constructors with different numbers of parameters depending on how precisely you wish to specify the time. We add the span on to the time and display the results. In displaying the results we use the ToLongDateString() method of the DateTime class to get the date in plain English, but the ToShortTimeString() method to get the time (using ToLongTimeString() would give us milliseconds as well). We then use various properties of the TimeSpan class to show the days, hours, minutes, seconds, milliseconds and ticks. Ticks are the smallest unit allowed by the TimeSpan class and measure one ten-millionth of a second, as indicated by a couple of static fields that give conversion factors, which we also display.

The above code gives this output:




5 RELATED COURSES AVAILABLE
C# PROGRAMMING
At the end of this course, you will have learned the fundamental skills that are required to design and develop o....
C++ PROGRAMMING
Object oriented programming is fast becoming the leading software design methodology, with C++ becoming ever more....
C PROGRAMMING
This course is design to provide non-C programmers with the essential skills and knowledge necessary to allow the....
C PROGRAMMING IN THE UNIX ENVIRONMENT
This course will provide readers the knowledge to use many of the UNIX 'C' library facilities, interface with the....
MICROSOFT VISUAL C++ 5 AND THE MFC&T 32 BIT WINDOWS PROGRAMMING
A complete 32 bit programming course highlighting the main features regarding the Microsoft Visual C++ programmin....
 
0 RELATED JOBS AVAILABLE
CONTACT US
Saturday 22nd November 2008  © COPYRIGHT 2008 - VISUALSOFT