可以将字符串日期格式转换为另一种字符串格式 [英] Can I Convert String Date Format To Another String Format
问题描述
您好先生,
我的问题是
我可以将字符串日期格式转换为另一种字符串格式吗?
string currentdate =04/28/2015(MM / dd / yyyy)
i想要在2015年4月28日格式化此日期( dd / MM / yyyy)
i问这个问题,因为在项目中我从Storedprocedure得到了这个dateformat(MM / dd / yyyy),我必须显示作为网格视图中的dd / MM / yyyy
而不更改storedprocedure因为这个sp用在最近的页面中
请告诉我怎么做?
给出一些解决方案
thanx
Hello sir,
My question Is
Can i Convert string date format to another string format ?
string currentdate= "04/28/2015" ("MM/dd/yyyy")
i want format this date in "28/04/2015" ("dd/MM/yyyy")
i ask this question because in project i got this dateformat("MM/dd/yyyy") from Storedprocedure and i have to display as"dd/MM/yyyy" in gridview
without changing storedprocedure because this sp used inalmost pages
please tell me how to do this ?
give some solution
thanx
推荐答案
使用表示数据而不是数据的字符串的整个想法本身是完全糟糕的,适得其反,但目前是穷人开发人员的一种非常典型的行为。不要这样走。使用类型System.DateTime
执行所有操作,而不是使用字符串。字符串只能在输出上,而不能在输入上。 (是的,序列化程序使用字符串,但它们应该使用一些固定的文化中立的往返字符串表示,最好是UCT
此类型的数字ToString
带有culture参数的方法(实现IFormatProvider>
)和/或表示格式的字符串参数:https://msdn.microsoft.com/en-us/library/system .datetime.tostring%28v = vs.110%29.aspx [ ^ ]。
格式请参阅:
https://msdn.microsoft.com/en- us / library / az4se3k1(v = vs.110).aspx [ ^ ],
https://msdn.microsoft.com/en-us/ library / 8kb3ddd4(v = vs.110).aspx [ ^ ]。
在极少数情况下,当你输入字符串表示时间时(你必须要做)确定它们真的很少见,你可以使用一些方法Parse
,ParseExact
,TryParse
或TryParseExact
: https://msdn.microsoft.com/en-us/library/system.datetime%28v=vs.110%29.aspx [ ^ ]。br />
-SA
The whole idea to work with strings representing data instead of data itself is utterly bad, counter-productive, but presently is a very typical behavior of poor developers. Don't go this way. Do everything with the typeSystem.DateTime
, not with the string. The string can only be on output and never on input. (Yes, serializers use strings, but they should use some fixed culture-neutral "round-trip" string representation, preferably UCT
This type has a number ofToString
method with the culture parameters parameters (implementingIFormatProvider>
) and/or string parameters representing format explicitly: https://msdn.microsoft.com/en-us/library/system.datetime.tostring%28v=vs.110%29.aspx[^].
For format, please see:
https://msdn.microsoft.com/en-us/library/az4se3k1(v=vs.110).aspx[^],
https://msdn.microsoft.com/en-us/library/8kb3ddd4(v=vs.110).aspx[^].
In rare cases when you have string representing time on input (you have to make sure they are really rare), you can use some of the methodsParse
,ParseExact
,TryParse
orTryParseExact
: https://msdn.microsoft.com/en-us/library/system.datetime%28v=vs.110%29.aspx[^].
—SA
string currentDate = "04/28/2015";
DateTime.ParseExact(currentDate, "MM/dd/yyyy").ToString("dd/MM/yyyy")
/ ravi
这篇关于可以将字符串日期格式转换为另一种字符串格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!