将日期格式转换为dd-MMM-yyy [英] convert date format into dd-MMM-yyy

查看:177
本文介绍了将日期格式转换为dd-MMM-yyy的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





在我的页面中,我有一个字符串格式的日期



Hi,

In my page i have a date with string format

string Schedule_Date_For_GFC =dr["Schedule_Date_For_GFC"].ToString()





我以17-6-2012的任何格式输入日期, 17-jun-2012,6-17-2012 ...



i希望将其转换为17-jun-2012。



这个我这样写的





I enter date in any format like 17-6-2012, 17-jun-2012,6-17-2012...

i want to be convert that into 17-jun-2012.

for this i wrote like this

string Schedule_Date_For_GFC =dr["Schedule_Date_For_GFC"].ToString("dd-MMM-yyyy");



但这里显示错误方法''tostring'没有重载''需要''1''参数。



这是我收到的错误信息。为此我在google找到解决方案首先我需要将该日期转换为datetime格式然后使用它。




but here it shows the error "no overload for method ''tostring'' takes ''1'' arguments".

this is the error message i got. for this i found solution in google first i need to convert that date into datetime format and then use this .

string Schedule_Date_For_GFC =((DateTime)dr["Schedule_Date_For_GFC"]).ToString("dd-MMM-yyyy");





这里它显示了与施法相关的错误....





如何克服这个问题....



任何人都建议我这个......



here it''s showing the error related to casting....


How to overcome this issue....

Any one suggest me regarding this...

推荐答案

首先,尝试尽可能使用 System.DateTime ,而不是使用字符串。



在您的情况下(请参阅我对问题的最后评论),您可以使用 System.DateTime.Parse

First of all, try to work with System.DateTime as much as possible, not with strings.

In your situation (please see my last comments to the question), you can use System.DateTime.Parse:
System.DateTime time = System.DateTime.Parse(dr["Schedule_Date_For_GFC"]).ToString());





因为存在字符串的可能性在第一个地方没有采用日期时间格式,您可以通过使用 System.DateTime.TryParse 来避免抛出异常。



最后,如果您确切地知道预期的格式,并且默认情况下解析不适合对于这种格式,你最好使用 System.DateTime.ParseExact System.DateTime.TryParseExact



请参阅:

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

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

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

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

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

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

http: //msdn.microsoft.com/en-us/library/h9b85w22.aspx [ ^ ]。



解决了这个问题。



祝你好运,

-SA



As the possibility exists that the string was not in a date-time format in first place, you can avoid throwing exception by using System.DateTime.TryParse instead.

And finally, if you know expected format exactly, and parsing by default is not suitable to this format, you should better use System.DateTime.ParseExact or System.DateTime.TryParseExact.

Please see:
http://msdn.microsoft.com/en-us/library/system.datetime.aspx[^],
http://msdn.microsoft.com/en-us/library/1k1skd40.aspx[^],
http://msdn.microsoft.com/en-us/library/w2sa9yss.aspx[^],
http://msdn.microsoft.com/en-us/library/332de853.aspx[^],
http://msdn.microsoft.com/en-us/library/ch92fbc1.aspx[^],
http://msdn.microsoft.com/en-us/library/ms131044.aspx[^],
http://msdn.microsoft.com/en-us/library/h9b85w22.aspx[^].

That solves the problem.

Good luck,

—SA


参考这个



http://anubhavg.wordpress.com/2009/06/11/how-to-format-datetime-date-in-sql-server-2005/ [<一个href =http://anubhavg.wordpress.com/2009/06/11/how-to-format-datetime-date-in-sql-server-2005/\"target =_ blanktitle =新窗口> ^ ]


嘿,





试试这个



hey,


try this

string Schedule_Date_For_GFC =dr["Schedule_Date_For_GFC"]).ToString("dd MMMM yyyy");  



您的答案如下所示:

2013年1月19日







希望这能帮到你


your answer will be look like below:
19 Jan 2013



hope this will help you


这篇关于将日期格式转换为dd-MMM-yyy的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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