日期设定和比较 [英] Date set and compare

查看:51
本文介绍了日期设定和比较的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在用户提供的DateTime变量中设置日期:

DateTime theDate;

theDate =" 08- 15-05" ;;


这给了我一个错误:


不能隐式地将类型''string''转换为''System.DateTime ''


如何设置它?


我想比较一下它是否等于另一个字符串变量

这也是一个日期:


如果theDate = stringDate ...


如何使用DateTime变量?


谢谢


Tom

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

推荐答案

试试这个......

theDate = System.DateTime.Parse(" 08-15-05");

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


" ; SONU" < KR ******* @ yahoo.com>在消息中写道

news:11 ********************** @ g47g2000cwa.googlegr oups.com ...
"sonu" <kr*******@yahoo.com> wrote in message
news:11**********************@g47g2000cwa.googlegr oups.com...
试试这个..
theDate = System.DateTime.Parse(" 08-15-05");
Try this..
theDate =System.DateTime.Parse("08-15-05");




这似乎有效。


但是如果我现在有一个字符串设置为日期,例如


sDate =" 09/15/05" < br $> b $ b或

s1Date =" 15-23-2005"


我如何将这些与theDate进行比较?我们在上面设置了什么?


谢谢,


Tom



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


tshad< ts**********@ftsolutions.com>写道:
tshad <ts**********@ftsolutions.com> wrote:
" sonu" < KR ******* @ yahoo.com>在消息中写道
新闻:11 ********************** @ g47g2000cwa.googlegr oups.com ...
"sonu" <kr*******@yahoo.com> wrote in message
news:11**********************@g47g2000cwa.googlegr oups.com...
试试这个..
theDate = System.DateTime.Parse(" 08-15-05");
Try this..
theDate =System.DateTime.Parse("08-15-05");



这似乎有效。
但是如果我现在有一个字符串设置为日期,例如

sDate =" 09/15/05"

s1Date =" 15-23- 2005

我如何将这些与theDate进行比较。我们在上面设置了什么?



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?




以相同的方式将另一个字符串转换为DateTime,并比较

两个DateTimes(例如使用= =)。


-

Jon Skeet - < sk *** @ pobox.com>
http://www.pobox.com/~skeet

如果回复小组,请不要给我发邮件



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


这篇关于日期设定和比较的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆