473,385 Members | 1,912 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Date set and compare

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
Nov 17 '05 #1
9 2993
Try this..
theDate =System.DateTime.Parse("08-15-05");

Nov 17 '05 #2
"sonu" <kr*******@yahoo.com> wrote in message
news:11**********************@g47g2000cwa.googlegr oups.com...
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
Nov 17 '05 #3
tshad <ts**********@ftsolutions.com> wrote:
"sonu" <kr*******@yahoo.com> wrote in message
news:11**********************@g47g2000cwa.googlegr oups.com...
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?


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

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 17 '05 #4
You might have some problems using "15-23-2005" ...
but thats just a thought :-p

Nov 17 '05 #5
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
Nov 17 '05 #6
Cor Ligthert [MVP] <no************@planet.nl> wrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 17 '05 #7
"Cor Ligthert [MVP]" <no************@planet.nl> wrote in message
news:Ov**************@tk2msftngp13.phx.gbl...
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 I hope this helps,

Cor

Nov 17 '05 #8
"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
tshad <ts**********@ftsolutions.com> wrote:
"sonu" <kr*******@yahoo.com> wrote in message
news:11**********************@g47g2000cwa.googlegr oups.com...
> 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?


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
--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Nov 17 '05 #9
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
Nov 17 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Gleep | last post by:
Hey Guys, I've got a table called Outcomes. With 3 columns and 15 rows 1st col 2nd col 3rdcol outcome date price There are 15 rows...
4
by: Richard Hollenbeck | last post by:
I'm trying to write some code that will convert any of the most popular standard date formats twice in to something like "dd Mmm yyyy" (i.e. 08 Jan 1908) and compare the first with the second and...
4
by: Stephen | last post by:
Is there any way of using a compare validator to compare a textbox to see if the date is greater than or equal to todays date. I want to set the ValueToCompare property to now(). It doesn't seem...
4
by: jty202 | last post by:
I have string that contains a date in this format (14-Jan-05). I want to store in date object if theres one and access each part of the date (month, year, dates, day of week). Specifically I...
7
by: James P. | last post by:
Hello there, In my asp.net page using VB, I have a date text field in mm/dd/yyyy format. When a date is entered, I'd like to validate it to make sure the date is greater than or equal to the...
4
by: blini | last post by:
Helo.... How I can convert string "26/03/2006 15:51" for a date? I need to convert and to compare if "09/06/2006 14:20" is lesser or equal that the current date. Everything in Javascript.
1
by: Budd | last post by:
Hi everyone i got a problem on date, it is... 1, i get the date from calendar component 2, compare today and selected date is equal (compare method) 3 count the number of day between this...
12
by: Assimalyst | last post by:
Hi, I have a working script that converts a dd/mm/yyyy text box date entry to yyyy/mm/dd and compares it to the current date, giving an error through an asp.net custom validator, it is as...
1
by: vrparekh | last post by:
in my webapplication,user pick date in dd/mm/yy format, and i want to compare that date with another date using compare validator but compare validator compare the dates using mm/dd/yy format. ...
2
by: deepikashalini | last post by:
hi, Im storing Issue date in database. Issuedate stored as varchar. and then compare to Passing string date('03/06/2008') what is query to compare storing varchar date to passing string...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.