Search Forum
(53671 Postings)
Search Site/Articles

Archived Articles
712 Articles

C# Books
C# Consultants
What Is C#?
Download Compiler
Code Archive
Archived Articles
C# Jobs
Beginners Tutorial
C# Contractors
C# Consulting
C# Manual
Contact Us

GoDiagram for .NET from Northwoods Software

Printable Version

Time Class
By Bostjan Maroh


The idea comes when i must create a project that work lots with time. So i have create time class. With this class you can calculate (sum) times, convert times from and to other data types.

Time class is writen in C# and .NET 2.0.

Time class explantation.

This is simple class and is not much to explain.

1.Public fields: 
 .public int Hour, 
 .public int Minute and 
 .public int Second. 
 .public const char TIME_SEPERATOR = ':'

 .current system time (public Time()), 
 .from string value (public Time(string value)), 
 .from parameters (public Time(int hour, int minute, int second)) and 
 .from seconds (public Time(int seconds)).

Public method Add:

Example 1:

InDoc.Systems.Time time1 = new InDoc.Systems.Time("12:00:55");
      // calculate 12:00:55 + 14:55:20 
      // result: 26:56:15

4.    To addition two times you can use + and to subtraction you can use -.
Example 2:
InDoc.Systems.Time time1 = new InDoc.Systems.Time("12:00:55") + 
new InDoc.Systems.Time("14:55:20");
      // result: 26:56:15
      InDoc.Systems.Time time2 = new InDoc.Systems.Time("14:55:20") . 
new InDoc.Systems.Time("12:00:55");
      // result: 02:54:25

5. We have some convert methods: 

 .public int ToSeconds(),
 .public override string ToString() 

and static method that convert secontd to Time object:
 .public static Time GetTimeFromSeconds(int seconds).

Download Source

If you have lots to do with times, you will save your time with this class. If you have any suggestions or questions let me know.