0

I have this code in vb:

dim d As Object
d = CDec((DateDiff("n", Format(StartTime), "hh:mm tt"), Format(EndTime), "hh:mm tt")) + 1) / 60)

Here, StartTime and EndTime is Date, and value is 09:30 AM and 06:30 PM. This gives me value 9.01666666666667

When i Converted this in C#:

object d;
d = decimal.Parse(((DateAndTime.DateDiff("n", StartTime.ToString("hh:mm tt"), EndTime.ToString("hh:mm tt")) + 1) / 60).ToString());

Here, StartTime and EndTime is DateTime and value is 09:30 and 18:30. This gives me value 9. I do not get a decimal value. Is there anything i am missing? Is it because the EndTime is in 24hrs DateTime format?

Toto
  • 33
  • 7
  • `object d;` why? use `var` – Zam Jan 11 '19 at 10:24
  • Why to declare `object d;` and not `decimal d;`? And what you mean _"I get no decimal value"_? – SᴇM Jan 11 '19 at 10:24
  • 5
    Try `/ 60.0` , DateDiff returns a 'long` and that means integer division (DIV) – bommelding Jan 11 '19 at 10:26
  • with `long t1 = 541; int t2 = 60;` obviously `t1/t2` = 9 – Selvin Jan 11 '19 at 10:26
  • The problem is integer division. See https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/operators/division-operator for some details but in essence if you take two integers and divide one by the other the result will always be an integer. If you want the result to have a decimal part convert one to a `decimal` or `double` or some other non integer type, such as by bommelding's suggestion. – Chris Jan 11 '19 at 10:27
  • 1
    Possible duplicate of [Why does integer division in C# return an integer and not a float?](https://stackoverflow.com/questions/10851273/why-does-integer-division-in-c-sharp-return-an-integer-and-not-a-float) – Selvin Jan 11 '19 at 10:29
  • 2
    Isn't 9 correct result, if times are exact 09:30 and 18:30? Anyway, to perform correct division result, you must specify 60 as decimal, which you can by using literal suffix M, e.g. `(DateAndTime.DateDiff("n", StartTime.ToString("hh:mm tt"), EndTime.ToString("hh:mm tt")) + 1) / 60M` – Darjan Bogdan Jan 11 '19 at 10:30
  • 1
    @DarjanBogdan: No. The times are 9*60 minutes apart. If you add 1 to that and divide by 60 you get 9 + 1/60. Why the OP is adding 1 I couldn't say but they are. :) – Chris Jan 11 '19 at 10:39
  • @Chris ah you are right, I didn't see +1 :D – Darjan Bogdan Jan 11 '19 at 10:46

1 Answers1

3

The problem you are having is integer division as you have been told in comments. That is an integer divided by an integer will always give an integer as a result. That's how things work in c#. To have a double as your final value one of the operands needs to be a double. In this case you could do that by specifying /60.0.

However if converting to c# you might as well use some better code than this direct translation. Here's some code that does the same (the first two lines will be omitted in yours since you already have your Date objects).

var startTime = new DateTime(2019,1,11,9,30,0);
var endTime = new DateTime(2019,1,11,18,30,0);

var d = ((endTime-startTime).TotalMinutes+1)/60;
Console.WriteLine(d);

Output:

9.01666666666667

And breaking it down:

endTime-startTime - In c# you can subtract a DateTime object from another and get a TimeSpan object.

.TotalMinutes - Timespan has a TotalMinutes property which is a double representing the total number of minutes of that Timespan - in this case that is returning 540 since our Timespan represents 9 hours or 540 minutes.

The addition and subtraction are pretty straightforward. Of note though is that the division is now a double divided by an integer so the result will be a double and thus not lose the decimal part as your original code did.

Chris
  • 27,210
  • 6
  • 71
  • 92