从VB中替换C#中的DateDiff [英] DateDiff replacement in C# from VB
问题描述
public string BindDisplayUntil(ref SqlDataReader rs,bool edit)
{
string html =;
if(edit)
{
html =< input type = text maxlength = 10 class = input size = 14 name = display_until_date id = display_until_date;
html + =value = \+ String.Format(rs [display_until_date]。ToString(),M / d / yyyy)。ToString()+\ >;
html + =;
html + =< img src = \images / icon-calendar.gif \align = \baseline \alt = \显示/隐藏日历\border = 0 width = \24 \height = \16 \>;
}
其他
{
html =< span> 0? style = \color:red; \:)+>;
html + = String.Format(rs [display_until_date]。ToString(), M / d / yyyy)。ToString()+;
}
返回html;
}
运行此代码时我有这个C#代码,出现以下错误
html =< span> 0? style = \color:red; \:)+>;
名称'DateDiff'不存在是当前上下文。
我需要在C#中替换'DateDiff'关键字
嗯...
html = < span > 0 ? style = \color:red; \: )+ >;
应该在span
之后的双引号?我不确定你到底想要做什么,因为VB代码看起来不应该编译...
从OP更新:
html = < span +(DateDiff( d,rs [ display_until_date],Now())< span class =code-keyword>> 0 ? style = \color:red; \: )+ >;
啊!
简单:
将返回值转换为DateTime(如果DB具有DATETIME或DATE值):
DateTime until(DateTime )rs [ display_until_date];
或者使用Parse你有它错了将它存储为字符串:
DateTime until = DateTime.Parse(rs [ < span class =code-string> display_until_date]。ToString());然后:
html = < span class =code-string> < span +(until - DateTime.Now).TotalDays > 0 ? style = \color:red; \: )+ >中跨度>;
public string BindDisplayUntil(ref SqlDataReader rs, bool edit)
{
string html = "";
if (edit)
{
html = "<input type=text maxlength=10 class=input size=14 name=display_until_date id=display_until_date";
html += " value=\"" + String.Format(rs["display_until_date"].ToString(), "M/d/yyyy").ToString() + "\">";
html += "";
html += "<img src=\"images/icon-calendar.gif\" align=\"baseline\" alt=\"Show/Hide Calendar\" border=0 width=\"24\" height=\"16\">";
}
else
{
html = "<span"> 0 ? " style=\"color:red;\"" : "") + ">";
html += String.Format(rs["display_until_date"].ToString(), "M/d/yyyy").ToString() + "";
}
return html;
}
I have this C# code when am running this code the following error arises
html = "<span"> 0 ? " style=\"color:red;\"" : "") + ">";
The name 'DateDiff' does not exist is current context.
The 'DateDiff' keyword i need to replace in C#
Um...
html = "<span"> 0 ? " style=\"color:red;\"" : "") + ">";
Should that double quote afterspan
be there? I'm not sure exactly what you are trying to do there, because the VB code doesn't look like it should compile either...
Update from OP:
html = "<span" + (DateDiff("d", rs["display_until_date"], Now()) > 0 ? " style=\"color:red;\"" : "") + ">";
Ah!
Simple:
Cast the return value as a DateTime (if you DB has DATETIME or DATE values):
DateTime until (DateTime) rs["display_until_date"];Or use Parse is you have got it wrong ans store it as a string:
DateTime until = DateTime.Parse(rs["display_until_date"].ToString());Then:
html = "<span" + (until - DateTime.Now).TotalDays > 0 ? " style=\"color:red;\"" : "") + ">";
这篇关于从VB中替换C#中的DateDiff的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!