By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,028 Members | 1,825 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,028 IT Pros & Developers. It's quick & easy.

compare dates

P: n/a
i have two date fields for event start date and end date. I let the user
enter two on create event page. on submit, i need to compare if the end date
is earlier than start date or not.

How can i compare two dates?

Thanks

Manny
Nov 19 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
"Manny Chohan" <Ma*********@discussions.microsoft.com> wrote in message news:8D**********************************@microsof t.com...
enter two on create event page. on submit, i need to compare if the end date
is earlier than start date or not.

How can i compare two dates?


Assuming these are TextBoxes, parse the Text in the TextBox into a
DateTime,

DateTime startDate = DateTime.Parse( startDateTextBox.Text);
// . . .

then compare the two DateTimes in an if statement,

if( endDate < startDate )
{
warningLabel.Text = "End date/time must be on or after Start date/time.";
}

If the text is not formatted correctly as a DateTime, then Parse( ) will
throw a FormatException (so as a practical matter, you should put that
code into a try...catch block).

You may also want to consider the effect of a user entering a date and/or
time in the format customary for other cultures (for instance, swapping
the sequence in which day and month are expected).

Derek Harmon
Nov 19 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.