- 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 get the difference between two dates
Use DateTime.Subtract to get the difference between two dates in C#.
Firstly, set two dates −
DateTime date1 = new DateTime(2018, 8, 27); DateTime date2 = new DateTime(2018, 8, 28);
Use the Subtract method to get the difference −
TimeSpan t = date2.Subtract(date1);
The following is the complete code −
Example
using System; using System.Threading; using System.Diagnostics; public class Demo { public static void Main() { DateTime date1 = new DateTime(2018, 8, 27); DateTime date2 = new DateTime(2018, 8, 28); // getting the difference TimeSpan t = date2.Subtract(date1); Console.WriteLine(t); Console.WriteLine("Days (Difference) = {0} ", t.TotalDays); Console.WriteLine("Minutes (Difference) = {0}", t.TotalMinutes); } }
Output
1.00:00:00 Days (Difference) = 1 Minutes (Difference) = 1440
- Related Articles
- C# Program to get the difference between two dates in seconds
- C# Program to determine the difference in hours between two dates
- How to get the difference between two dates in Android?
- Not able to get the difference between two dates (SAP)
- Java Program to get milliseconds between dates
- How to get the differences between two dates in iOS?
- How to calculate the difference between two dates in JavaScript?
- Get the difference between dates and calculate salary with MySQL?
- How to get the number of seconds between two Dates in JavaScript?
- How to get the number of days between two Dates in JavaScript?
- How to get the differences between two dates in Android using Kotlin?
- C# Program to return the difference between two sequences
- C# program to list the difference between two lists
- Java Program to get the difference between two time zones by seconds
- How to get number of quarters between two dates in Java

Advertisements