将代码日期与SQL日期ASP NET进行比较

时间:2017-06-13 14:38:07

标签: sql asp.net vb.net

当我尝试比较时,我收到有关转换类型日期的错误:

页面加载:

Dim DataCorrenteLoad As String = DateTime.Now.ToString("dd-MM-yyyy")
Label2.Text = DataCorrenteLoad

并且在SqlDataSource中where条件是FinishDate> =今天日期

我该如何解决这种情况?我应该改变格式dd-mm-yyyy吗?或者是什么 ?

感谢。

2 个答案:

答案 0 :(得分:2)

似乎,您想要比较日期,但您将重点放在日期的字符串表示上。这不一样!

看看例子:

Dim DatabaseDate As DateTime = DateTime.Today.AddDays(-5)
Dim CurrentDate As DateTime = DateTime.Today
 'case 1: compare and display date in current culture - depending on regional settings
Console.WriteLine("{0} {1} equal to {2}", DatabaseDate.ToString("d"), If(DatabaseDate=CurrentDate, "is", "is not") , CurrentDate.ToString("d"))

'case 2: change string representation of date:
'compare dates in France and German format
Dim cu1 As Globalization.CultureInfo = New Globalization.CultureInfo("Fr-fr")
Dim cu2 As Globalization.CultureInfo = New Globalization.CultureInfo("De-de")
DatabaseDate = DateTime.Today
Console.WriteLine("{0} {1} equal to {2}", DatabaseDate.ToString("d", cu1), If(DatabaseDate=CurrentDate, "is", "is not") , CurrentDate.ToString("d", cu2))

以上代码返回:

'case 1
2017-06-08 is not equal to 2017-06-13
'case 2
13/06/2017 is equal to 13.06.2017

<强>结论:
根据操作系统区域设置,日期格式可能不同,但日期仍然相同!

有关详细信息,我强烈建议您阅读以下内容:
Standard Date and Time Format Strings
Custom Date and Time Format Strings
Parsing Date and Time Strings in .NET
Design and Implementation Guidelines for Web Clients
Formatting Date and Time for a Specific Culture

答案 1 :(得分:0)

Label2.Text = System.DateTime.Now.ToString(“dd-mm-yyyy”);

使用它将工作System.DateTime.Now将显示当你使用To strong(“dd-mm-yyyy”)时的当前时间它会将其转换为字符串并设置它是你用双引号设置的格式。 / p>