Answered by:
VB DateDiff in C#

Question
-
User320184449 posted
Anyone know the best way to convert this to C# from VB, "Dim MinutesPast As Integer = DateDiff("n", dr("DateTime"), Now)"? I tried a couple code converters but they're not giving me anything that works. Thanks!
Monday, October 26, 2020 3:42 PM
Answers
-
User475983607 posted
Anyone know the best way to convert this to C# from VB, "Dim MinutesPast As Integer = DateDiff("n", dr("DateTime"), Now)"? I tried a couple code converters but they're not giving me anything that works. Thanks!
DateTime Date1 = DateTime.Now.AddDays(-5); TimeSpan diff = DateTime.Now - Date1; Console.WriteLine(diff.TotalMinutes);
Works the same in VB.NET.
Reference
https://docs.microsoft.com/en-us/dotnet/api/system.timespan?view=netcore-3.1
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Monday, October 26, 2020 4:43 PM -
User409696431 posted
What are the variables you are passing to DateDiff? What is "n", and what is "dr(DateTime)"?
In C# you would use TimeSpan
Are you looking for the number of days between two dates?
DateTime date1 = new DateTime(2020, 1, 1); DateTime date2 = DateTime.Today; int daysDiff = ((TimeSpan)(date2 - date1)).Days;
Minutes between two times? (This ignores date part, just looks at time.)
DateTime date1 = new DateTime(2020, 1, 1); DateTime date2b = DateTime.Now; int daysDiffmin = ((TimeSpan)(date2b - date1)).Minutes;
Total minutes between two dates? (Can't be int because it includes fractional minutes introduced by the seconds part of the datetime.)
double daysDifftotmin = ((TimeSpan)(date2b - date1)).TotalMinutes;
If you want the total minutes between two dates in int, you can add the first two results together.
...And so on. TimeSpan has lots of options. See: https://docs.microsoft.com/en-us/dotnet/api/system.timespan?view=netframework-4.8
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Monday, October 26, 2020 4:53 PM
All replies
-
User475983607 posted
Anyone know the best way to convert this to C# from VB, "Dim MinutesPast As Integer = DateDiff("n", dr("DateTime"), Now)"? I tried a couple code converters but they're not giving me anything that works. Thanks!
DateTime Date1 = DateTime.Now.AddDays(-5); TimeSpan diff = DateTime.Now - Date1; Console.WriteLine(diff.TotalMinutes);
Works the same in VB.NET.
Reference
https://docs.microsoft.com/en-us/dotnet/api/system.timespan?view=netcore-3.1
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Monday, October 26, 2020 4:43 PM -
User320184449 posted
Yeah I seen that but I was hoping I could do it in a single line of code like I can do in VB. Having trouble getting that example implemented though, do I need to cast the SqlDataReader somehow? Trying this but it's not working:
DateTime date1 = dr["DateTime"];
TimeSpan diff = DateTime.Now - date1;Monday, October 26, 2020 4:52 PM -
User409696431 posted
What are the variables you are passing to DateDiff? What is "n", and what is "dr(DateTime)"?
In C# you would use TimeSpan
Are you looking for the number of days between two dates?
DateTime date1 = new DateTime(2020, 1, 1); DateTime date2 = DateTime.Today; int daysDiff = ((TimeSpan)(date2 - date1)).Days;
Minutes between two times? (This ignores date part, just looks at time.)
DateTime date1 = new DateTime(2020, 1, 1); DateTime date2b = DateTime.Now; int daysDiffmin = ((TimeSpan)(date2b - date1)).Minutes;
Total minutes between two dates? (Can't be int because it includes fractional minutes introduced by the seconds part of the datetime.)
double daysDifftotmin = ((TimeSpan)(date2b - date1)).TotalMinutes;
If you want the total minutes between two dates in int, you can add the first two results together.
...And so on. TimeSpan has lots of options. See: https://docs.microsoft.com/en-us/dotnet/api/system.timespan?view=netframework-4.8
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Monday, October 26, 2020 4:53 PM -
User320184449 posted
The "n" is a minute format:
https://www.w3schools.com/asp/func_datediff.aspDo you know how to cast an SqlDataReader into the DateTime in C#?
Monday, October 26, 2020 4:57 PM -
User320184449 posted
Oh wait, VisualStudio shows the fix: "DateTime date1 = (DateTime)dr["DateTime"];" Look at me, I'm learning C#..
Monday, October 26, 2020 5:03 PM -
User409696431 posted
Visual Studio gives excellent hints. You do have to be in the ballpark of the right code.
Monday, October 26, 2020 5:10 PM -
User-821857111 posted
You always have to cast the DataReader to the correct type before you attempt any type-specific operation on it. The indexer returns an object type.
Monday, October 26, 2020 5:12 PM -
User320184449 posted
Thank you both for making me smarter!
Monday, October 26, 2020 5:13 PM