Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Why is my date calculation not displaying anything?

I’m trying to use c# to get the number of days in-between two dates.

Here is my little program:

public class Program
{
    public static void Main()
    {
            DateTime today = DateTime.Now;
            DateTime futureDate = DateTime.Today.AddDays(3);
            int daysBetween = (int)Math.Round((futureDate - today).TotalDays);
            
            Console.WriteLine("Date testing\n\n");
        
            Console.WriteLine("Today: {0:MM/dd/yy H:mm}", today);
            Console.WriteLine("futureDate: {0:MM/dd/yy H:mm}", futureDate);
            Console.WriteLine("daysBetween: ", daysBetween);
    }
}

This is what I am getting.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Date testing
 
 
Today: 04/01/22 14:10 
futureDate: 04/04/22 0:00 
dateNumber:

For some reason, dateNumber is blank, but I am expecting it to be 3.

Is there a way to get that number?

Thanks!

>Solution :

You are printing a formatted string. The {0} means to insert the first parameter following the format string;

 Console.WriteLine("daysBetween: {0} ", daysBetween);
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading