Skip to content
Loading
  • Dinesh Gabhane
    0
    Also check below link
     
    http://csharp.net-informations.com/statements/csharp-date-difference.htm 
    0
  • Rupesh Kahane
    0
    1. public static void Main()  
    2.     {  
    3.       
    4.         var timespan = DateTime.Now - new DateTime(2019,10,7);    
    5.         int days = timespan.Days;         
    6.         // Format your output  
    7.         var output = String.Format("{0} days, {1} hours and {2} minutes have passed",timespan.Days, timespan.Hours, timespan.Minutes);  
    8.         Console.WriteLine(output);  
    9.            
    10.         DateTime dtByUser = DateTime.Parse("2019,10,7").Date;     
    11.         var todaysDate = DateTime.Now.Date;  
    12.         var diff = todaysDate.Subtract(dtByUser);  
    13.         Console.WriteLine();  
    14.         Console.WriteLine(diff);  
    15.     }  
    Refer above code, you can directly get days difference.
     
    Note: For date difference you need to fomat your string to DateTime
     
    For more formats in C# refer the links
     
    1) https://docs.microsoft.com/en-us/dotnet/standard/base-types/standard-date-and-time-format-strings
     
    2) https://docs.microsoft.com/en-us/dotnet/standard/base-types/custom-date-and-time-format-strings
     
    3) http://www.csharp-examples.net/string-format-datetime/
     
    4) https://www.c-sharpcorner.com/article/date-time-formats-used-in-c-sharp-mvc/ 
    0
  • 0
    Thanks you both for you suggestions.
     
    Actually I am trying to create a DLL by using a class library function. Here is my function:-
     
    using System;
    namespace Date_Difference
    {
    public class Program
    {
    public int DateDifference(DateTime x, DateTime y)
    {
    DateTime start = x;
    DateTime end = y;
    TimeSpan difference = end - start;
    return difference.Days;
    }

    }
    }
     
    However when I build my solution then I get a DLL and when I used in my Automation Anywhere RPA tool then it gives me the following error messaege:-
     
     
    It seems my tool doesn't have a method to pass the Date time format it can only send the input in string and can receive the output in string.
     
    So is it possible to change the input and output in string instead of the datetime.. If it is possible.. then please let me know how to do that. 
    0
  • Mageshwaran R
    0
    Hi,
    Try this Code:
    1. System.TimeSpan diffResult = (EndDate - StartDate).TotalDays  
     
    0
  • 0
    using System; namespace Date_Difference { class Program { static void Main(string[] args) { string z = "2018, 11, 06"; string a = "2019, 11, 11";Console.WriteLine(DateDifference(z, a)); Console.ReadLine();} public static string DateDifference(string x, string y) { DateTime XX=DateTime.Parse(x).Date; DateTime YY=DateTime.Parse(y).Date; Var difference = (YY - XX); return difference.ToString(); } } }
    0