将字符串转换为datetime错误 [英] Convert string to datetime error

查看:84
本文介绍了将字符串转换为datetime错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人可以告诉我这段代码有什么问题



  string  s =  @ ;  //   s = - ;  
string r = HttpUtility.HtmlDecode(s);
string r3 = string .Format( 1 {0} Jan {0} 2007,r);

string format = d-MMM-YYYY;
DateTime MinedDateTime;
bool result = DateTime.TryParseExact(r3.Trim(),format, null ,DateTimeStyles .None, out MinedDateTime);
Console.WriteLine(r3);

if (r3 == 1-Jan-2007
{
Console.WriteLine( 相等);
Console.WriteLine( 1-Jan-2007);
}
else
{
Console.WriteLine( 不等于);
Console.WriteLine( 1-Jan-2007);
}
Console.WriteLine(结果);





输出是

2007年1月1日

不等于

1-Jan-2007

False







但当我更换

string.Format(1 {0} Jan {0} 2007,r);

with

string.Format(1 {0} Jan {0} 2007, - );

一切正常



输出是

1-Jan-2007

等于

1 -Jan-2007

True



s = html代码'' - ''(hypen)未显示

解决方案

你正在做一个最初常见的初学者错误之一:尝试使用数据的字符串表示而不是数据。



这不是显而易见的:显示相同时间或相同数据的两个时间点仍然相同,即使它们以不同的字符串表示形式显示,由于d

不同的格式/文化?比较时间,而不是字符串:
System.DateTime thisInstance = System.DateTime.Now;
System.DateTime someOtherTime = // ..别的东西
if (thisInstance == someOtherTime) // ...你可以使用等价运算符
//
if (thisInstance > = someOtherTime) // ......依此类推





为什么,为什么要比较代表时间或日期的字符串,永远?



您需要这些字符串,例如,当您需要在屏幕上显示它们时。



首先,了解所有 System.DateTime.ToString 方法:http://msdn.microsoft.com/en-us/library/system.datetime.aspx [ ^ ]。



其次,学习格式:

http:// msdn.microsoft.com/en-us/library/zdtaw1bw.aspx [ ^ ],

http: //msdn.microsoft.com/en-us/library/az4se3k1.aspx [ ^ ],

http://msdn.microsoft.com/en-us/library/8kb3ddd4.asp x [ ^ ]。



这就是全部。



-SA

Can anybody tell me what wrong with this code

string s =@"­";  //s = "-";
string r = HttpUtility.HtmlDecode(s);
string r3 = string.Format("1{0}Jan{0}2007",r);

string format = "d-MMM-yyyy";
DateTime MinedDateTime ; 
bool result = DateTime.TryParseExact(r3.Trim(), format, null, DateTimeStyles.None, out MinedDateTime);
Console.WriteLine(r3);

if(r3 == "1-Jan-2007")
{
	Console.WriteLine("Equal");
	Console.WriteLine("1-Jan-2007");
}
else
{
	Console.WriteLine("Not Equal");
	Console.WriteLine("1-Jan-2007");
}
Console.WriteLine(result);



the Output is
1-Jan-2007
Not Equal
1-Jan-2007
False



but when i replace
string.Format("1{0}Jan{0}2007",r);
with
string.Format("1{0}Jan{0}2007","-");
every thing works fine

the Output is
1-Jan-2007
Equal
1-Jan-2007
True

s= html code of ''-''(hypen) is not showing

解决方案

You are doing one of the worse of the very usual mistakes of beginners this days: trying to work with string representation of data instead of data.

Isn''t this obvious: two points of times showing identical time or identical data are still identical, even if they are shown in different string representations, due to d

ifferent formats/cultures? Compare time, not strings:
System.DateTime thisInstance = System.DateTime.Now;
System.DateTime someOtherTime = // .. something else
if (thisInstance == someOtherTime) // ... you can use equivalence operator
// or
if (thisInstance >= someOtherTime) // ... and so on



Why, why comparing strings representing time or date, ever?

You need those strings, for example, when you need to show them on screen.

First, learn about all System.DateTime.ToString methods: http://msdn.microsoft.com/en-us/library/system.datetime.aspx[^].

Second, learn formatting:
http://msdn.microsoft.com/en-us/library/zdtaw1bw.aspx[^],
http://msdn.microsoft.com/en-us/library/az4se3k1.aspx[^],
http://msdn.microsoft.com/en-us/library/8kb3ddd4.aspx[^].

That''s all.

—SA


这篇关于将字符串转换为datetime错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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