-1

I am trying to get the timespan of 2 date and time, but i want it to be in only the value of counting the hours before converting to double...

static void Main() {
    string tin = "Nov 08, 2021 4:23 PM";
    string tout = "Nov 10, 2021 4:23 AM";
            DateTime t1 = DateTime.Parse(tin);
            DateTime t2 = DateTime.Parse(tout);
            TimeSpan Workingtime = t2 - t1;
            string time = Workingtime.ToString();
            string[] timesplit = time.Split(':');
            double num = double.Parse(timesplit[0]) + (double.Parse(timesplit[1]) / 60);

    Console.Write(num);
}
Mark
  • 23
  • 4
  • `Workingtime.Hours` or `Workingtime.TotalHours`? – gunr2171 Nov 04 '21 at 13:11
  • Are you looking for [`Workingtime.TotalHours`](https://learn.microsoft.com/en-us/dotnet/api/system.timespan.totalhours?view=net-5.0)? – Lasse V. Karlsen Nov 04 '21 at 13:11
  • Does this answer your question? [Showing Difference between two datetime values in hours](https://stackoverflow.com/questions/4946316/showing-difference-between-two-datetime-values-in-hours) – gunr2171 Nov 04 '21 at 13:12

1 Answers1

0

I think you're looking for something like this:

//Example returns 24 hours
var d2 = DateTime.Now.AddDays(-1);
var d1 = DateTime.Now;
var dif = d1.Subtract(d2).TotalHours; //Returns difference in hours
A-Fairooz
  • 410
  • 3
  • 11