0

I have 2 textboxes with 2 ajax calendars.One is start date and the other is end date. I'm trying to find the days between them but I can't seem to get it right.This is how I do it:

string a = txt1.Text;
        string b = txt2.Text;


        DateTime data1 = Convert.ToDateTime(a);
        DateTime data2 = Convert.ToDateTime(b);

        TimeSpan span = data2 - data1;

        int zile = Convert.ToInt32(Math.Ceiling(span.TotalDays));

        label.Text = span.ToString();

The dates from my textboxes are in this format: 4/4/2012 and 4/5/2012. This should be one day difference but when I run this code it brings up 30 day because it's interpreted as one month..How can I change that???

Bibu
  • 201
  • 3
  • 10
  • 25

2 Answers2

1

This worked for me, just tested it.

using System;

namespace DaySpan
{
    class Program
    {
        static void Main(string[] args)
        {

            DateTime startDate = DateTime.Parse("03.04.2012");
            DateTime endDate = DateTime.Parse("06.04.2012");

            Console.WriteLine(startDate.ToString());
            Console.WriteLine(endDate.ToString());

            Console.WriteLine("--------------------------------");
            Console.WriteLine("Calculate dayspan:");

            TimeSpan span = endDate - startDate;

            Console.WriteLine("Span: " + span.TotalDays);

            Console.ReadLine();

        }
    } 
}

EDIT: This may though a problem as mentioned in top post with datetime parsing and locale. However this is correct with my locale (norwegian).

scheien
  • 2,457
  • 1
  • 19
  • 22
0

Doesn't that result indicate 92 days 0 hrs, 0 mins, 0 secs? I guess I don't see the problem here.

Edit - If you are wanting that to give you just the days property, take a look at this SO post

Community
  • 1
  • 1
LJ Wilson
  • 14,445
  • 5
  • 38
  • 62