
- C# Basic Tutorial
- C# - Home
- C# - Overview
- C# - Environment
- C# - Program Structure
- C# - Basic Syntax
- C# - Data Types
- C# - Type Conversion
- C# - Variables
- C# - Constants
- C# - Operators
- C# - Decision Making
- C# - Loops
- C# - Encapsulation
- C# - Methods
- C# - Nullables
- C# - Arrays
- C# - Strings
- C# - Structure
- C# - Enums
- C# - Classes
- C# - Inheritance
- C# - Polymorphism
- C# - Operator Overloading
- C# - Interfaces
- C# - Namespaces
- C# - Preprocessor Directives
- C# - Regular Expressions
- C# - Exception Handling
- C# - File I/O
- C# Advanced Tutorial
- C# - Attributes
- C# - Reflection
- C# - Properties
- C# - Indexers
- C# - Delegates
- C# - Events
- C# - Collections
- C# - Generics
- C# - Anonymous Methods
- C# - Unsafe Codes
- C# - Multithreading
- C# Useful Resources
- C# - Questions and Answers
- C# - Quick Guide
- C# - Useful Resources
- C# - Discussion
C# Program to get the difference between two dates in seconds
Set two dates.
DateTime date1 = new DateTime(2018, 7, 15, 08, 15, 20); DateTime date2 = new DateTime(2018, 7, 15, 11, 14, 25);
Now calculate the difference between two dates.
TimeSpan ts = date2 - date1;
Move further and calculate the difference in seconds.
ts.TotalSeconds
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, 7, 15, 11, 14, 25); TimeSpan ts = date2 - date1; Console.WriteLine("No. of Seconds (Difference) = {0}", ts.TotalSeconds); } }
Output
No. of Seconds (Difference) = 10745
- Related Articles
- C# Program to get the difference between two dates
- How to get the number of seconds between two Dates in JavaScript?
- C# Program to determine the difference in hours between two dates
- How to get the difference between two dates in Android?
- Java Program to get the difference between two time zones by seconds
- Not able to get the difference between two dates (SAP)
- Get the difference between two timestamps in seconds in MySQL?
- How to get time difference between two timestamps in seconds?
- How to get the differences between two dates in iOS?
- Java Program to get milliseconds between dates
- Get the relative timestamp difference between dates in JavaScript
- How to calculate the difference between two dates in JavaScript?
- How to get the differences between two dates in Android using Kotlin?
- How to get the number of days between two Dates in JavaScript?
- MySQL difference between two timestamps in Seconds?

Advertisements