C Sharp/TimeSpan

From Meshplex

Jump to: navigation, search
Image:Csharp_programming.gif

Main Home

Basics
C# Tutorial Home
C# - Introduction to Visual Studio IDE
Introduction to C#
C# - Overview
C# - Statements
C# - Data Types
C# - Variables
C# - Operators
C# - Flow Control
C# - Variables II
C# - Functions and Methods
C# - Classes and Objects I
C# - Enumerations
C# - Dates and Times

C# - Random Numbers

Advanced
C# - Inheritance
C# - Polymorphism
C# - Garbage Collection
C# - Operator Overloading
C# - Encapsulation
C# - Properties
C# - Indexers
C# - Exceptions
C# - GUI
C# - Delegates
C# - Events
C# - Components
C# - Multithreading
C# - Regular Expressions
C# - Graphics and Multimedia
C# - Files and Streams
C# - XML
C# - Database, SQL and ADO.NET
C# - ASP.NET Web Forms and Web Controls
C# - Web Services
C# - Network Programming
C# - Datastructures and Collections
C# - Enumerations and Iterators
C# - .NET Assemblies
C# - CLR
C# - Visual Studio Debugger
C# - Namespaces
C# - Generics
C# - MS Intermediate Language
C# - Deploying Windows Application

[edit] Introducing C# TimeSpan

TimeSpan gives the ability for C# to calculate the amount of time between two datetime objects. TimeSpan also has the functionality to add and subtract a given time interval.

  • There are 5 overloaded timespan versions
  • you can only add and subtract TimeSpan objects with Datetime objects
TimeSpan s = new TimeSpan(2,5,12);

This is how the TimeSpan object is created. Remember, that there are 5 versions of this overloaded class so you don't have to use just this version. Take a look at the following example:

Example 1: This code will simply add a time interval to a datetime class

using System;
class Program
{
    static void Main()
    {
        //One hour interval
        TimeSpan MyTimeSpan = new TimeSpan(1, 0, 0);   
        int year = 1;
        int month = 1;
        int day = 1;
        int hour = 3;
        int min = 30;
        int sec = 0;
        DateTime MyDate = new DateTime(year, month, day, hour, min, sec);
 
        //print ticks
        Console.WriteLine(MyDate.ToString("H:mm:ss"));
        MyDate += MyTimeSpan;   //Add one hour to the current time
        Console.WriteLine(MyDate.ToString("H:mm:ss"));
        Console.Read();
    }
}

Output:

3:30:0
4:30:0

As you can see using the TimeSpan class is simple to use

[edit] C# TimeSpan Properties

Property Return Type Description
Days int Returns the number of days from a TimeSpan
Hours int Returns the number of hours from a TimeSpan
Milliseconds int Returns the number of Milliseconds a TimeSpan
Minutes int Returns the number of Minutes from a TimeSpan
Seconds int Returns the number of seconds from a TimeSpan
Ticks long Returns the number of ticks in a TimeSpan
TotalDays double Returns the total number of days in a TimeSpan
TotalHours double Returns the total number of hours in a TimeSpan
TotalMilliseconds double Returns the total number of Milliseconds of a TimeSpan
TotalMinutes double Returns the total number of Minutes in a TimeSpan
TotalSeconds double Returns the total number of seconds in a TimeSpan