DateDiff方法是C# [英] DateDiff Method is C#
本文介绍了DateDiff方法是C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
Hello all
我需要在C#中替换DateDiff方法.....我已经将一些vb代码翻译成C#并且出错了DateDiff ...
以下是我的VB代码
如果 rs( test_date)> Now()然后
sb.Append(DateDiff( d,Now(),rs( test_date))& ; 几天之后)
其他
以下是C#代码
if (Convert.ToDateTime(rs [ test_date])> DateTime.Now)
{
sb.Append(DateDiff( d,DateTime.Now,rs [ test_date])+ 几天之后);
}
错误:
sb.Append(DateDiff(d,DateTime。现在,rs [test_date])+几天之后);
错误名称:当前上下文中不存在DateDiff这个名称。
你们都被要求让我替换这个...
谢谢
解决方案
使用 TimeSpan 而不是DateDiff [ ^ ]:
TimeSpan diff =(DateTime)rs [ test_date] - DateTime.Now;
int daysAway = diff.TotalDays;
sb.AppendFormat( {0}天之后,daysAway);
好吧,我不会这样做,但我想你可以添加一个引用
命名空间:Microsoft.VisualBasic
模块:DateAndTime
程序集:Visual Basic Runtime库(在Microsoft.VisualBasic.dll中)
https://msdn.microsoft.com/en-us/library/b5xbyt6f(v = vs.90).aspx [ ^ ]
Hello all
I need to have a replacement of DateDiff method in C#..... I have translate some vb code into C# and have an error in DateDiff...
Below is my VB Code
If rs("test_date") > Now() Then
sb.Append(DateDiff("d", Now(), rs("test_date")) & " days away")
Else
Below is C# code
if (Convert.ToDateTime(rs["test_date"]) > DateTime.Now)
{
sb.Append(DateDiff("d", DateTime.Now, rs["test_date"]) + " days away");
}
Error:
sb.Append(DateDiff("d", DateTime.Now, rs["test_date"]) + " days away");
error name: The name 'DateDiff' does not exists in current context.
You all are requested to let me have a replacement of this...
Thanks
解决方案
Instead of DateDiff, use a TimeSpan[^]:
TimeSpan diff = (DateTime)rs["test_date"] - DateTime.Now; int daysAway = diff.TotalDays; sb.AppendFormat("{0} days away", daysAway);
Well, I wouldn't do it, but I suppose you could add a reference to
Namespace: Microsoft.VisualBasic
Module: DateAndTime
Assembly: Visual Basic Runtime Library (in Microsoft.VisualBasic.dll)
https://msdn.microsoft.com/en-us/library/b5xbyt6f(v=vs.90).aspx[^]
这篇关于DateDiff方法是C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文