Difference between two DateTimes C# ?

Difference between two DateTimes C# ?

Asked on December 22, 2018 in C#.
Add Comment


  • 6 Answer(s)

    Try the code below,

    TimeSpan duration = b - a;
    

    There is lot of built in methods in the timespan class.

    For example,

    duration.TotalSeconds
    duration.TotalMinutes
    

    For more reference about Timespan struct

    Answered on December 22, 2018.
    Add Comment

    Follow the procedure:

    Initially try this code,

    double hours = (b-a).TotalHours;
    
    

    Rather than the difference in days, If you need the hour difference then use this,

    int hours = (b-a).Hours;
    
    

    The time difference is more than 1 day is the main difference between these two properties that means hours and dates. The Hours property will report the actual hour difference between the two dates. So in some cases two dates differed by 100 years and occurred at the same time in the day, hours gives the output 0. The difference between in the total amount of hours that occurred between the two dates should returned by TotalHours .

    And another main difference is , the fractional hours will returned by the TotalHours .  If it is not happened, Math.Round can adjust like these things.

    Answered on December 22, 2018.
    Add Comment

    Simply use this code,

    int Difference = (a-b).Hours;
    
    Answered on December 22, 2018.
    Add Comment

    The time difference b/w to time will be shown use this method.

     private void HoursCalculator()
        {
            var t1 = txtfromtime.Text.Trim();
            var t2 = txttotime.Text.Trim();
            var Fromtime = t1.Substring(6);
            var Totime = t2.Substring(6);
            if (Fromtime == "M")
            {
                 Fromtime = t1.Substring(5);
            }
            if (Totime == "M")
            {
                Totime = t2.Substring(5);
            }
    
            if (Fromtime=="PM" && Totime=="AM" )
            {
                var dt1 = DateTime.Parse("1900-01-01 " + txtfromtime.Text.Trim());
                var dt2 = DateTime.Parse("1900-01-02 " + txttotime.Text.Trim());
                var t = dt1.Subtract(dt2);
                //int temp = Convert.ToInt32(t.Hours);
                //temp = temp / 2;
                lblHours.Text =t.Hours.ToString() + ":" + t.Minutes.ToString();
    
            }
            else if (Fromtime == "AM" && Totime == "PM")
            {
                var dt1 = DateTime.Parse("1900-01-01 " + txtfromtime.Text.Trim());
                var dt2 = DateTime.Parse("1900-01-01 " + txttotime.Text.Trim());
                TimeSpan t = (dt2.Subtract(dt1));
                lblHours.Text = t.Hours.ToString() + ":" + t.Minutes.ToString();
            }
            else
            {
                var dt1 = DateTime.Parse("1900-01-01 " + txtfromtime.Text.Trim());
                var dt2 = DateTime.Parse("1900-01-01 " + txttotime.Text.Trim());
                TimeSpan t = (dt2.Subtract(dt1));
                lblHours.Text = t.Hours.ToString() + ":" + t.Minutes.ToString();
            }
        }
    Answered on January 15, 2019.
    Add Comment

    This article introduces the various options for calculating the date difference in C#.

    • DateTime Structure: This datatype is used to represent a time instant in C#
    • TimeSpan Structure: The TimeSpan datatype is used to represent time interval
    1. using System;
    2. using System.Collections.Generic;
    3. using System.Text;
    4. namespace Console_DateTime
    5. {
    6.     class Program
    7.     {
    8.         static void Main(string[] args)
    9.         {
    10.             System.DateTime dtTodayNoon = new System.DateTime(2018, 9, 13, 12, 0, 0);
    11.             System.DateTime dtTodayMidnight = new System.DateTime(2018, 9, 12, 0, 0, 0);
    12.             System.TimeSpan diffResult = dtTodayNoon.Subtract(dtTodayMidnight);
    13.             Console.WriteLine($“Yesterday Midnight – Today Noon = {diffResult.Days}”);
    14.             Console.WriteLine($“Yesterday Midnight – Today Noon = {diffResult.TotalDays}”);
    15.             Console.ReadLine();
    16.         }
    17.     }
    18. }
    Answered on January 15, 2019.
    Add Comment
    int hours = (int)Math.Round((b - a).TotalHours)
    Answered on January 28, 2019.
    Add Comment


  • Your Answer

    By posting your answer, you agree to the privacy policy and terms of service.