使用#eval()的Gridview绑定中的日期格式问题 [英] Date Format Issue in Gridview binding with #eval()
问题描述
你好先生,
i am绑定gridview但我的问题是:
i希望以这种格式约会
hello sir,
i am binding gridview but my issue is :
i want to date with this format
<% #Eval("contractdate", "{0:dd/MM/yyyy}") %>
但是我每次都有MM / dd / yyyy格式的日期
i知道从这个问题来的地方
在存储过程中有一个转换为此完成contractdate为MM / dd / yyyy
但我无法修改此sp coz这个sp用于项目的其他页面
所以我的问题是
我可以在eval中转换日期格式而不更改sp吗?
如果是,那么如何?
因为我在google上搜索但没有任何想法
thanx。
but i got date everytime MM/dd/yyyy format
i know that from where this issue comes
in stored procedure there is a convertion done for this "contractdate" as "MM/dd/yyyy"
but i cant modify this sp coz this sp is used at other pages in project
so my question is
can i convert date format in eval without changing sp ?
if yes then how ?
coz i search on google but not got any idea
thanx.
推荐答案
假设您没有任何空白日期,并且他们总是 格式化为MM / dd / yyyy,这应该有效:
Assuming you don't have any blank dates, and they're always formatted as "MM/dd/yyyy", this should work:
<%# DateTime.ParseExact(Eval("contractdate", "{0}"), "MM/dd/yyyy", System.Globalization.CultureInfo.InvariantCulture).ToString("dd/MM/yyyy", System.Globalization.CultureInfo.CurrentCulture) %>
通过在代码中使用函数,可以使其更具弹性和整理效果-behind:
You could make this more resilient and tidy it up by using a function in your code-behind:
using System.Globalization;
...
public static string ReformatDate(string valueFromDatabase)
{
DateTime value;
if (!DateTime.TryParseExact(valueFromDatabase, "MM/dd/yyyy", CultureInfo.InvariantCulture, DateTimeStyles.None, out value))
{
return string.Empty;
}
return value.ToString("dd/MM/yyyy", CultureInfo.CurrentCulture);
}
<%# ReformatDate(Eval("contractdate", "{0}")) %>
这篇关于使用#eval()的Gridview绑定中的日期格式问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!