compare dates

G

Guest

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
 
D

Derek Harmon

Manny Chohan said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top