terrible-coder
terrible-coder

Reputation: 333

Converting System Date Format to Date Format Acceptable to DateTime in C#

How can I convert a system date format (like 3/18/2014) to the format readable in DateTime? I wanted to get the total days from two dates, which will come from two TextBoxes.

I have tried this syntax:

DateTime tempDateBorrowed = DateTime.Parse(txtDateBorrowed.Text);
DateTime tempReturnDate = DateTime.Parse(txtReturnDate.Text);
TimeSpan span = DateTime.Today - tempDateBorrowed;
rf.txtDaysBorrowed.Text = span.ToString();

But tempDateBorrowed always returns the minimum date for a DateTime varibale. I think this is because DateTime does not properly parse my system date format. As a consequence, it incorrectly displays the number of days. For example, if I try to enter 3/17/2014 and 3/18/2014 respectively, I always get -365241 days instead of 1.

Edit: I wanted my locale to be non-specific so I did not set a specific locale for my date format. (My system format by the way is en-US)

Upvotes: 0

Views: 842

Answers (6)

nashcheez
nashcheez

Reputation: 5183

Try this:

if(DateTime.TryParseExact(txtDateBorrowed.Text, "M/d/yyyy", CultureInfo.InvariantCulture, DateTimeStyles.None, out tempDateBorrowed))
{
    TimeSpan span = DateTime.Today - tempDateBorrowed;
}

Upvotes: 0

Sam
Sam

Reputation: 2917

Try DateTime.ParseExact method instead.

See following sample code (I've used strings instead of TextBoxes since I used a Console app to write this code). Hope this helps.

class Program
    {
        static void Main(string[] args)
        {
            string txtDateBorrowed = "3/17/2014";
            string txtReturnDate = "3/18/2014";
            string txtDaysBorrowed = string.Empty;

            DateTime tempDateBorrowed = DateTime.ParseExact(txtDateBorrowed, "M/d/yyyy", null);
            DateTime tempReturnDate = DateTime.ParseExact(txtReturnDate, "M/d/yyyy", null);
            TimeSpan span = DateTime.Today - tempDateBorrowed;
            txtDaysBorrowed = span.ToString();
        }
    }

Upvotes: 1

John Earnshaw
John Earnshaw

Reputation: 331

try formatting your date to iso 8601 or something like that before parsing it with DateTime.Parse.

2014-03-17T00:00:00 should work with DateTime.Parse. ("yyyy-MM-ddTHH:mm:ssZ")

Upvotes: 0

Grant Winney
Grant Winney

Reputation: 66439

My first thought is to just replace the TextBox controls with a DateTimePicker or equivalent, depending on what platform you're developing on. Converting strings to dates or vice-versa is more of a pain than it seems at first.


Or you could try using DateTime.ParseExact instead, to specify the exact expected format:

DateTime tempDateBorrowed =
    DateTime.ParseExact("3/17/2014", "M/dd/yyyy", CultureInfo.InvariantCulture);

Or you could specify a specific culture in the call to DateTime.Parse:

var tempDateBorrowed = DateTime.Parse("17/3/2014", new CultureInfo("en-gb"));

var tempDateBorrowed = DateTime.Parse("3/17/2014", new CultureInfo("en-us"));

Upvotes: 0

vbn
vbn

Reputation: 799

You can try specifying the format of the datetime in the textboxes like this

DateTime tempDateBorrowed = DateTime.ParseExact(txtDateBorrowed.Text.Trim(), "M/d/yyyy", CultureInfo.InvariantCulture);
DateTime tempReturnDate = DateTime.ParseExact(txtReturnDate.Text.Trim(), "M/d/yyyy", CultureInfo.InvariantCulture);

Also you may have to check if the values from the textboxes are valid.

Upvotes: 0

paparazzo
paparazzo

Reputation: 45096

ToString is not Days

TimeSpan.TotalDays Property

Upvotes: 1

Related Questions