C# : Everything about DateTime

This example is does most of required operation with respect to time. You can add two times, convert to seconds, get the differences, parse the time in common format and much more.

Complete C# code: just copy paste and it will start working.
------------------------------------------------------------

using System;

namespace MyDateTimeApplication
{
class Program
{
static void Main(string[] args)
{
MyDateTime time1 = new MyDateTime();
MyDateTime time2 = new MyDateTime(20, 50, 25);
Console.WriteLine("Current time: " + time1.Hour + ":" + time1.Minute + ":" + time1.Second + " Or " + MyDateTime.Now());
Console.WriteLine("Add time: " + time1.Add("12:45:20"));        
Console.WriteLine("Time difference MyDateTime values: " + MyDateTime.TimeDiff(time2, time1));
Console.WriteLine("Time difference string values: " + MyDateTime.TimeDiff("12:34:50", "13:20:34"));
Console.WriteLine("Convert current time to seconds: " + MyDateTime.ToSeconds(DateTime.Now));
Console.WriteLine("Parse string time" + MyDateTime.Parse("23:50:12"));
Console.WriteLine("My time to seconds: " + time1.ToSeconds());
Console.WriteLine("My time to string: " + time1.ToString());
Console.Read();        
}
}

public class MyDateTime
{
#region Public constants

public const char TIME_SEPERATOR = ':';

#endregion

        #region Declarations

public int Hour;
public int Minute;
public int Second;

#endregion

        #region Constructors

public MyDateTime()
{
Hour = DateTime.Now.Hour;
Minute = DateTime.Now.Minute;
Second = DateTime.Now.Second;
}

public MyDateTime(string value)
{
string[] vals = value.Split(TIME_SEPERATOR);
Hour = int.Parse(vals[0]);
Minute = int.Parse(vals[1]);

if (vals.Length > 2)
Second = int.Parse(vals[2]);

new MyDateTime(this.ToSeconds());
}

public MyDateTime(int hour, int minute, int second)
{
Hour = hour;
Minute = minute;
Second = second;
new MyDateTime(this.ToSeconds());
}

public MyDateTime(int seconds)
{
Minute = seconds / 60;
Second = seconds % 60;
Hour = Minute / 60;
Minute = Minute % 60;
}

#endregion

        #region Public methods

public MyDateTime Add(MyDateTime time)
{
this.Hour += time.Hour;
this.Minute += time.Minute;
this.Second += time.Second;

return new MyDateTime(GetStringTime(this.ToSeconds()));
}

public MyDateTime Add(string value)
{
return Add(new MyDateTime(value));
}

#endregion

        #region Public static methods

public static MyDateTime Now()
{
DateTime dt = DateTime.Now;
return GetTimeFromSeconds(ToSeconds(dt));
}

public static MyDateTime TimeDiff(MyDateTime time1, MyDateTime time2)
{
try
            {
                 int _secs1 = time1.ToSeconds();
int _secs2 = time2.ToSeconds();

int _secs = _secs1 - _secs2;

return GetTimeFromSeconds(_secs);
}
catch
            {
                 return new MyDateTime(0, 0, 0);
}

}

public static MyDateTime TimeDiff(string time1, string time2)
{
try
            {
                MyDateTime t1 = new MyDateTime(time1);
MyDateTime t2 = new MyDateTime(time2);
return TimeDiff(t1, t2);
}
catch
            {
                 return new MyDateTime(0, 0, 0);
}
}

public static MyDateTime TimeDiff(DateTime dateTime1, DateTime dateTime2)
{
try
            {
                TimeSpan span = dateTime1 - dateTime2;
                 return new MyDateTime(span.Seconds);
}
catch
            {
                 return new MyDateTime(0, 0, 0);
}
}

public static MyDateTime TimeDiff(int seconds1, int seconds2)
{
try
            {
                MyDateTime t1 = new MyDateTime(seconds1);
MyDateTime t2 = new MyDateTime(seconds2);
return TimeDiff(t1, t2);
}
catch
            {
                 return new MyDateTime(0, 0, 0);
}
}

#endregion

        #region Convert methods

public int ToSeconds()
{
return this.Hour * 3600 + this.Minute * 60 + this.Second;
}

public static int ToSeconds(DateTime dateTime)
{
return dateTime.Hour * 3600 + dateTime.Minute * 60 + dateTime.Second;
}

public override string ToString()
{
return String.Format("{0:00}:{1:00}:{2:00}", Hour, Minute, Second);
}

public static MyDateTime GetTimeFromSeconds(int seconds)
{
int _mins = seconds / 60;
seconds = seconds % 60;

int _hours = _mins / 60;
_mins = _mins % 60;

return new MyDateTime(_hours, _mins, seconds);
}

private string GetStringTime(int seconds)
{
int _mins = seconds / 60;
seconds = seconds % 60;

int _hours = _mins / 60;
_mins = _mins % 60;

this.Hour = _hours;
this.Minute = _mins;
this.Second = seconds;

return String.Format("{0:00}:{1:00}:{2:00}", _hours, _mins, seconds); ;
}

public static MyDateTime Parse(string value)
{
try
            {
                 return new MyDateTime(value);
}
catch
            {
                 throw new ApplicationException("Error parsing time!");
}
}

#endregion

        #region Subtract time objects

public static MyDateTime operator +(MyDateTime t1, MyDateTime t2)
{
MyDateTime t3 = new MyDateTime(t1.Hour, t1.Minute, t1.Second);
t3.Add(t2);
return t3;
}

public static MyDateTime operator -(MyDateTime t1, MyDateTime t2)
{
return TimeDiff(t1, t2);
}

#endregion
    }
}

Output:
-------
Current time: 13:55:7 Or 13:55:07
Add time: 26:40:27
Time difference MyDateTime values: -05:-50:-02
Time difference string values: 00:-45:-44
Convert current time to seconds: 50107
Parse string time23:50:12
My time to seconds: 96027
My time to string: 26:40:27

Regards,
Megha
By Perry    Popularity  (10051 Views)