How to get the number of seconds between two dates in C#

2 Answers

0 votes
using System;

class SecondsBetweenTwoDates
{
    static void Main()
    {
        TimeSpan diff = (DateTime.Now - new DateTime(1966, 02, 27));
 
        Console.WriteLine(diff.TotalSeconds);
    }
}



/*
run:

1858091541.08731
   
*/


answered Apr 8, 2014 by avibootz
edited Jan 13, 2025 by avibootz
0 votes
using System;

class SecondsBetweenTwoDates
{
    static void Main()
    {
        DateTime startDate = new DateTime(2025, 1, 12);
        DateTime endDate =   new DateTime(2025, 1, 13);

        // Calculate the difference between the two dates
        TimeSpan timeDifference = endDate - startDate;

        // Get the total number of seconds
        double totalSeconds = timeDifference.TotalSeconds;

        Console.WriteLine($"The number of seconds between the two dates is: {totalSeconds}");
    }
}



/*
run:

The number of seconds between the two dates is: 86400
   
*/

 



answered Jan 13, 2025 by avibootz
edited Jan 13, 2025 by avibootz

Related questions

1 answer 88 views
1 answer 107 views
1 answer 113 views
1 answer 105 views
1 answer 99 views
1 answer 102 views
2 answers 119 views
...