Date set and compare

T

tshad

I am trying to set a date in a DateTime variable that I get from a user:

DateTime theDate;
theDate = "08-15-05";

This gives me an error:

Cannot implicitly convert type 'string' to 'System.DateTime'

How do I set it?

I then want to compare it to see if it is equal to another string variable
that is also a date:

if theDate = stringDate...

How do I do this with the DateTime variable?

Thanks

Tom
 
T

tshad

sonu said:
Try this..
theDate =System.DateTime.Parse("08-15-05");

That seems to work.

But if I now have a string set to a date, such as

sDate = "09/15/05"
or
s1Date = "15-23-2005"

How would I compare these to "theDate" that we set above?

Thanks,

Tom
 
J

Jon Skeet [C# MVP]

tshad said:
That seems to work.

But if I now have a string set to a date, such as

sDate = "09/15/05"
or
s1Date = "15-23-2005"

How would I compare these to "theDate" that we set above?

Convert the other string to a DateTime in the same way, and compare the
two DateTimes (eg using ==).
 
C

Cor Ligthert [MVP]

In addition to Jon,

If you compare Dates than it is easy to do it as this

if (myDate.Ticks == theOtherDate.Ticks)

I hope this helps,

Cor
 
J

Jon Skeet [C# MVP]

Cor Ligthert said:
In addition to Jon,

If you compare Dates than it is easy to do it as this

if (myDate.Ticks == theOtherDate.Ticks)

I hope this helps,

It's easier than that - it's as easy as:

if (myDate == theOtherDate)

because == is overloaded for DateTimes.
 
T

tshad

Jon Skeet said:
Convert the other string to a DateTime in the same way, and compare the
two DateTimes (eg using ==).

That would make sense, as the conversion would check to make sure it is a
valid date as well.

Thanks,

Tom
 
T

tshad

Cor Ligthert said:
In addition to Jon,

If you compare Dates than it is easy to do it as this

if (myDate.Ticks == theOtherDate.Ticks)

That's true, as well as day, month and year.

Thanks,

Tom
 
C

Cor Ligthert [MVP]

Jon,
It's easier than that - it's as easy as:

if (myDate == theOtherDate)

because == is overloaded for DateTimes.

You are right, I obviously are mixing some things up.


Cor
 

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

Top