为什么我的datetime.parseexact有错误? [英] Why my datetime.parseexact has error?

查看:92
本文介绍了为什么我的datetime.parseexact有错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

ava的价值是{12/12/2015 12:00:00 AM}



我的尝试:



ateTime ava = Convert.ToDateTime(theRow.Cells [3] .Text);

DateTime to = Convert.ToDateTime(theRow.Cells [ 4]。文本);

Response.Redirect(UpdateLoanType.aspx?type =+ type +& term =+ term +& rate =+ rate +& ava = + ava +& to =+ to);





DateTime nowed = DateTime.ParseExact(Request.QueryString [ava ],dd / MM / yyyy,CultureInfo.InvariantCulture);

解决方案

按照上述2个解决方案或只是在将格式传递给查询时更改格式string ...



  string  ava = Convert.ToDateTime(theRow。单元格[ 3 ]。文本).ToString(  dd /月/年); 
string to = Convert.ToDateTime(theRow.Cells [ 4 ]。Text).ToString ( dd / MM / yyyy);





这应该在目标页面中有效:

 DateTime nowed = DateTime.ParseExact(Request.QueryString [  ava],  dd / MM / yyyy,CultureInfo.InvariantCulture); 


那么你的输入字符串是

 12/12/2015 12:00:00 AM 



和你的解析表达式是

 dd / MM / yyyy 



所以你的表达式与输入字符串不匹配。



请尝试使用此表达式:

 dd / MM / yyyy hh:mm:ss tt 





[更新]

DateTime没有特定格式,它是日期的占位符,时间。

仅当您决定向用户提供日期时,例如一个文本框,你会给它一个格式。

为此你可以使用方法:

- ToLongDateString()

- ToLongTimeString( )

- ToShortDateString()

- ToShortTimeString()

- ToString()



ToString()将为您提供最大程度的格式控制,您可以避免不同计算机上日期的文化变化,除非需要。

参见自定义日期和时间格式字符串 [ ^ ]



对于您想要的格式,它将是

 DateTime dt = DateTime.Now; 
string s = dt.ToString( dd / MM / yyyy);



所以请忘记以特定格式存储日期。

唯一的方法这样做是将它存储为字符串,这不是最聪明的事情。


因为该值与您提供的模式不完全匹配:

 12/12/2015 12:00:00 AM 

不符合:

 dd / MM / yyyy 

匹配:

 dd / MM / yyyy hh:mm:ss tt 


value for ava is {12/12/2015 12:00:00 AM}

What I have tried:

ateTime ava = Convert.ToDateTime(theRow.Cells[3].Text);
DateTime to = Convert.ToDateTime(theRow.Cells[4].Text);
Response.Redirect("UpdateLoanType.aspx?type=" + type+"&term="+term+"&rate="+rate+"&ava="+ava+"&to="+to);


DateTime nowed = DateTime.ParseExact(Request.QueryString["ava"], "dd/MM/yyyy", CultureInfo.InvariantCulture);

解决方案

Follow the above 2 solutions or just change the format while you are passing it to the query string..

string ava = Convert.ToDateTime(theRow.Cells[3].Text).ToString("dd/MM/yyyy");
            string to = Convert.ToDateTime(theRow.Cells[4].Text).ToString("dd/MM/yyyy");  



and this should work in the target page:

DateTime nowed = DateTime.ParseExact(Request.QueryString["ava"], "dd/MM/yyyy", CultureInfo.InvariantCulture);


Well your input string is

12/12/2015 12:00:00 AM


and your parse expression is

dd/MM/yyyy


so your expression doesn't match the input string.

Try this expression instead:

dd/MM/yyyy hh:mm:ss tt



[UPDATE]
DateTime does not have a specific format, it is kind of a placeholder for a date and time.
Only when you decide to present the date to a user, in e.g. a text box, will you give it a format.
For this you can use the methods:
- ToLongDateString()
- ToLongTimeString()
- ToShortDateString()
- ToShortTimeString()
- ToString()

ToString() will give you the most control of the format and you can avoid culture variations of the date on different computers, unless that is desired.
See Custom Date and Time Format Strings[^]

For your desired format it would be

DateTime dt = DateTime.Now;
string s = dt.ToString("dd/MM/yyyy");


So please forget about storing a date in a specific format.
The only way to do that is to store it as a string, and that would not be the smartest of things.


Because the value doesn't exactly match the pattern you supply:

12/12/2015 12:00:00 AM

Does not match:

dd/MM/yyyy

It does match:

dd/MM/yyyy hh:mm:ss tt


这篇关于为什么我的datetime.parseexact有错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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