Datetime in C#

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I have 2 problem:

1.Ex: textbox1:dd/mm/yyyy
textbox2:dd/mm/yyyy.

Now, i want to compare these,such as i require users must type into textbox
which the date in textbox2 is lager than the second one.
So how do i do? Please tell me.
2.
The textbox must formatted datetime type(dd/mm/yyyy), but users can type
mm/yyyy or yyyy.Is there any way to do this in C#? If not how way to do that?
 
I have 2 problem:

1.Ex: textbox1:dd/mm/yyyy
textbox2:dd/mm/yyyy.

Now, i want to compare these,such as i require users must type into
textbox
which the date in textbox2 is lager than the second one.
So how do i do? Please tell me.
2.
The textbox must formatted datetime type(dd/mm/yyyy), but users can type
mm/yyyy or yyyy.Is there any way to do this in C#? If not how way to do
that?

Why not use a DateTimePicker instead of a TextBox?

this.dateTimePicker1.Format =
System.Windows.Forms.DateTimePickerFormat.Custom;

this.dateTimePicker1.CustomFormat =
"dd/MM/yyyy";


This also makes it easier for you to compare the dates from the two boxes.

if(this.dateTimePicker1.Value > this.dateTimePicker2.Value)
{
// Actions when the date 1 is lower than date 2
}

....as you get the dates as DateTime objects from the start, without the need
to parse/convert...


// Bjorn A
 
Bjorn,

Just an addition
if(this.dateTimePicker1.Value > this.dateTimePicker2.Value)
{
// Actions when the date 1 is lower than date 2

if(this.dateTimePicker1.Value.Date > this.dateTimePicker2.Value.Date)

Otherwise there will be forever a difference.

:-)

Cor
 
Bjorn,

Just an addition

Oops, I meant "higher" instead of "lower"..
if(this.dateTimePicker1.Value.Date > this.dateTimePicker2.Value.Date)

Otherwise there will be forever a difference.

On a *very* slow machine, possibly... ;-)

But, you're of course right that it's better to use only the Date-part, as
it's up to the user to change the dates arbitrarily, and the Time-part
*could* cause problems...


// BJorn A
 
Bjorn,

AFAIK, can you not set two datetimepickers by hand on the same time, it is
accurate in ticks.

Cor
 
...
AFAIK, can you not set two datetimepickers by hand on the same time, it is
accurate in ticks.

You're absolutely correct.

That's why I support your suggestion to use only the Date-part.

However, if the user *doesn't* change the default values at all from
instantiation, both will have the same tick values. At least on my
machine... ;-)

// Bjorn A
 
You could use regular expressions to check the format of the string the user
input,and get the information of the date at the same time.
Declear a regular expression object and assigned the expression as
"([0-9]{2})/([0-9]{2})/([0-9]{4})".Then use the IsMatch() method to check if
the format is true,and Group() method to get the "day","month" and "year".
 
Back
Top