- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
C# Program to determine the difference in hours between two dates
Set two dates.
DateTime date1 = new DateTime(2018, 7, 15, 08, 15, 20); DateTime date2 = new DateTime(2018, 8, 17, 11, 14, 25);
Now, get the difference between two dates.
TimeSpan ts = date2 - date1;
Get the result i.e. the difference in hours.
ts.TotalHours
Let us see the complete code.
Example
using System; using System.Linq; public class Demo { public static void Main() { DateTime date1 = new DateTime(2018, 7, 15, 08, 15, 20); DateTime date2 = new DateTime(2018, 8, 17, 11, 14, 25); TimeSpan ts = date2 - date1; Console.WriteLine("No. of Hours (Difference) = {0}", ts.TotalHours); } }
Output
No. of Hours (Difference) = 794.984722222222
- Related Articles
- C# Program to get the difference between two dates
- C# Program to get the difference between two dates in seconds
- How to get the difference between two dates in Android?
- How to calculate the difference between two dates in JavaScript?
- Not able to get the difference between two dates (SAP)
- Calculate minutes between two dates in C#
- C# Program to return the difference between two sequences
- C# program to list the difference between two lists
- How to list all dates between two dates in Excel?
- C Program to calculate the difference between two time periods
- Python program to find number of days between two given dates
- C++ Program to Calculate Difference Between Two Time Period
- Calculate the difference between two dates in days, weeks, months and years in Excel
- PHP program to compare two dates
- How to create a vector with dates between two dates in R?

Advertisements