In arithmetic operation for DateTime a method called
DateTime.AddDays Method (Double)
Using this method could easy if we want use only days with out fraction (for example Complete days that will refer in double parameter with integer numbers) or common fractions of days (for example it will refer into double parameter with 0.5 for half or 0.25 for eighth)
For me I think it will be issue if I asked to supplied double parameter with a day have a value like (12 days 5 hours 30 minutes 54 second 235 milliseconds)
Translating this into fraction could have sort of hardness and could be wasting of time
So I decided to build a custom method (In my Intermediate level in C#) that give my instance value for the double according to specific value for parameters as:
public static double TimeToFraction(int day, int hour, int minute, int second, int millisecond)
{
var hour_to_date = TimeSpan.FromHours(hour).TotalDays;
var min_to_day = TimeSpan.FromMinutes(minute).TotalDays;
var second_to_day = TimeSpan.FromSeconds(second).TotalDays;
var millisec_to_day = TimeSpan.FromMilliseconds(millisecond).TotalDays;
return day+ hour_to_date + min_to_day + second_to_day + millisec_to_day;
}
and it try it in the following code and its work
class Program
{
public static void Main()
{
var GetDate = new DateTime(2000, 01, 01, 00, 00, 00, 000);
// Set The value
var dd = Program.TimeToFraction(1, 3, 00, 0, 0);
// Using The regular way
var resukt = GetDate.AddDays(1.125);
// using my method
var result = GetDate.AddDays(dd);
Console.WriteLine(resukt.ToString("yyyy'/'MM'/'dd HH:mm:ss.fffff"));
Console.WriteLine(result.ToString("yyyy'/'MM'/'dd HH:mm:ss.fffff"));
// outputs
//2000/01/02 03:00:00.00000
//2000/01/02 03:00:00.00000
Console.ReadLine();
}
public static double TimeToFraction(int day, int hour, int minute, int second, int millisecond)
{
var hour_to_date = TimeSpan.FromHours(hour).TotalDays;
var min_to_day = TimeSpan.FromMinutes(minute).TotalDays;
var second_to_day = TimeSpan.FromSeconds(second).TotalDays;
var millisec_to_day = TimeSpan.FromMilliseconds(millisecond).TotalDays;
return day+ hour_to_date + min_to_day + second_to_day + millisec_to_day;
}
}
I just want to hear form you guys if what I did is good or not? any Idea about this.. or code improving for the method..