-1

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..

Ammar
  • 91
  • 2
  • 13
  • 2
    Questions for code reviews should be posted on [Code Review Stack Exchange](https://www.google.com/url?sa=t&rct=j&q=&esrc=s&source=web&cd=1&cad=rja&uact=8&ved=2ahUKEwiV1f308O3cAhUKKHwKHaZwCNUQFjAAegQIAhAC&url=https%3A%2F%2Fcodereview.stackexchange.com%2F&usg=AOvVaw2iukwx3qbrF9xz9ALJ4Kil) – Rufus L Aug 15 '18 at 01:15
  • 3
    Why wouldn't someone just use the `DateTime.Add` method with a `TimeSpan` overload? In your example, it would be: `var result = GetDate.Add(new TimeSpan(1, 3, 0, 0, 0));` – Rufus L Aug 15 '18 at 01:19
  • 1
    By the way, assuming your code is working, it might be better suited to [Code Review](https://codereview.stackexchange.com/help/on-topic). – ProgrammingLlama Aug 15 '18 at 01:31
  • @RufusL L- I'm not aware of this site – Ammar Aug 15 '18 at 02:59

2 Answers2

2

This method can be entirely replaced with TimeSpan. TimeSpan can be used like this:

var ts = new TimeSpan(days, hours, minutes, seconds, milliseconds);

To use it as in your example:

var dd = new TimeSpan(1, 3, 00, 0, 0);
// using my method 
var result = GetDate.Add(dd);

Using TimeSpan is also safer because you're not adding a series of floating point numbers together. This is because TimeSpan actually converts the input to milliseconds and then ticks without ever using floating point numbers, so you're guaranteed to add the time you actually entered. That said, I can't find a case where your method breaks because of this.

ProgrammingLlama
  • 36,677
  • 7
  • 67
  • 86
1

You can simplify your method just like that:

public static void Main()
{
    var GetDate = new DateTime(2000, 01, 01, 00, 00, 00, 000);

    // Using The regular way 
    var resukt = GetDate.AddDays(1.125);
    // using my method 
    var result = GetDate.Add(1, 3, 00, 0, 0);

    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 class DatimeExtensions
{
    public static DateTime Add(this DateTime date, int day, int hour, int minute, int second, int millisecond) =>
        date.AddDays(day).AddHours(hour).AddMinutes(minute).AddSeconds(second).AddMilliseconds(millisecond);
}

Let me know if it helps you.

Victor Coll
  • 119
  • 1
  • 3