Find The Difference Of Day, Hour, Minutes, Seconds, In C#

Hello Techies,

Once again I am back with some cool and easy ways to find the difference between two dates in the C#. Today, we are not going to discuss too much about the other things like how we were used to finding the difference before I will just directly move to the concept which we are going to use today.

So, we have a DateTime Variable, one is the start date and the second one is the end date. So, now the situation is something like that the second date on which we want to perform the difference operations is in the string format and the current date is in the DateTime Format. Then what?

So, the simple solution is to Conver it or parse it to the DateTime it is simple right. So, let's just check out the following snippet to check how the parsing is going to be done for the string to DateTime.

string stringDate = "8/6/2021 6:19:02 AM";  //If Date is in String Format..  
DateTime dateFromString = DateTime.Parse(stringDate); //Parse the String to the DateTime

Now, you will see we got the date on which we want to perform the operations now what about the current date. So, let's check with that also. Follow the following snippet to get the current date.

DateTime startTime = DateTime.Now; //Current Date

Ok, so now we got the current date also. Now, you might think like that what is the short form or the easier form here. am I right? Ok, so let me just check with you you think I am going to just subtract the date like (Date1 - Date2 ) and store it to another DateTime variable. So, not a pretty good idea to do that. So, we are using the time span to perform the subtract operation and to get them all differences.

So, for that check out the following snippet,

TimeSpan span = startTime.Subtract ( dateFromString ); //Perform to Find the Difference

Ok, so now you have checked to declare the variable of time span and store the value of the difference now what. So, firstly I will explain TimeSpan in two lines. So, TimeSpan is a class that has properties like Days, Hours, Minutes, Seconds, Milliseconds. Ok, so if you are familiar with the properties of the class then you will understand this thing properly. So, properties are one time of object which is set or return the value of the particular variable.

So, now in our case the startTime.Subtract perform the operation and return the DateTime values which are Day, Hour, Minute, MiliSecond, Seconds. Which become stores inside the TimeSpan and we can easily get those from the TimeSpan easy or not?

Ok, let me just tell you how you will get all these values from the TimeSpan. Check out the following the Code Snippet.

int Secondsdiff=span.Seconds;
int Minutesdiff=span.Minutes;
int Hoursdiff=span.Hours;
int Daysdiff=span.Days;

Ok, so we can get the properties value by its name with the object of the TimeSpan class.

And you can use these values wherever you want like as a timer or something like that how much time days or hours left to reach some date.

Let's just check the following full source code or you can check with the link given here: Compile the Code

using System;

class DifferenceBetweenTwoDates 
{
  static void Main() 
  {
      DateTime startTime = DateTime.Now; //Current Date
      Console.WriteLine(startTime);
      
      string dateString = "8/6/2021 6:19:02 AM";  //If Date is in String Format..
      
      DateTime dateFromString = DateTime.Parse(dateString); //Parse the String to the DateTime
      
      //DateTime endTime = new DateTime(2021, 6, 8, 6, 01, 20); 
      //Date in the(yyyy,dd,mm,hh,mm,ss) format
      
      TimeSpan span = startTime.Subtract ( dateFromString );
      
      int Secondsdiff=span.Seconds;
      int Minutesdiff=span.Minutes;
      int Hoursdiff=span.Hours;
      int Daysdiff=span.Days;
      
      Console.WriteLine( "Time Difference (seconds): " + Secondsdiff.ToString() );
      Console.WriteLine( "Time Difference (minutes): " + Minutesdiff.ToString() );
      Console.WriteLine( "Time Difference (hours): " + Hoursdiff.ToString() );
      Console.WriteLine( "Time Difference (days): " + Daysdiff.ToString() );
  }
}

Thank you, Techies, I Hope this will be helpful for you.

Happy Coding.


Similar Articles