我有一个日期时间数据类型 12/30/2015 11:30:00 AM
所以,在 SQL 服务器上,它将是 fromDate.ToString ("yyyy-MM- dd HH: mm tt")
结果是 2016-01-01 12: 30: 00,000
并且我只想将字符串数据部分剪切为几天、几小时和几分钟
最佳答案
改变你的
.ToString ("yyyy-MM-dd HH: mm tt")
//showing: year (yyyy), month (MM), day of month (dd), hour (HH), minute (mm), and "The AM/PM designator" (tt)
到
.ToString ("dd HH:mm")
//showing: day of month (dd), hour (HH), and minute (mm) only
More关于 DateTime
格式,您可能会发现它非常有用。解释是用方便的样本给出的,非常完整。
编辑:
要从 SQL
解释此类文本数据,请使用 .Substring(int index, int length)
string text = sqltext.Substring(("2016-01-").Length, 9); //the 9 must be from your days to your minutes. If the text string is shorter/longer, change this value
这里是对两个Substring
参数的解释:
index
参数从文本的0
偏移量开始。假设您的文本是
2016-01-01 12:30:00,000
那么index = 0
将指向第一个2
,index = 1
到第一个0
等等。要获取日期部分的第一个数字0
,根据您的输入,您需要有7
的索引。但更简单的方法来确定index
是通过获取 allstring
before 你想要的位置并捕获它的长度
(这是我显示的:“2016-01-”)。长度
。length
参数给出了您要从index
点向前获取的字符数。因为你想要
01 12: 30
包含总共9
个字符(注意你的white spaces
)你应该把9
。或者你也可以通过放置("01 12: 30").Length
来更通用:string text = sqltext.Substring(("2016-01-").Length, ("01 12: 30").Length);
Mr. James 的补充说明(见评论):“请注意,为这个答案给出的“天数”是 day of the month ,在任何情况下都不能超过 31 ......如果你想要从某个基准日期开始的天数,你会必须执行 DateTime
减法并使用 TimeSpan.ToString
。”
关于c# - 如何在 ASP.Net 中切割日期时间数据类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34522927/