比较VB.Net中的两个日期? [英] Comparing two dates in VB.Net?

查看:775
本文介绍了比较VB.Net中的两个日期?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个代码比较gridview到date的日期。今天

但它似乎不起作用





I have this code that compare a date from gridview to date.today
but it seems not working


"select * from out_logtable  order by outid desc limit 1")
DataGridView1.DataSource = Me.BindingSource1
DataGridView1.AutoGenerateColumns = True
Dim x As String = DateAndTime.Today.ToString("d")
Dim y As String = DataGridView1.Rows(0).Cells(2).Value.ToString
If DataGridView1.Rows(0).Cells(1).Value <> "admin" Then
    If x.ToString > y.ToString Then
        ExecuteSQL(1, "Update login_table set hour = '2' ")
    End If
Else

End If

推荐答案

比较两个日期?不,你没有。您必须编写比较两个STRINGS的代码,而不是日期。比较是不一样的。



将值转换为DateTime,而不是字符串。
Compares two dates?? No you don't. You have to code that compares two STRINGS, not dates. The comparison is not the same.

Convert the values to DateTime, not Strings.


继续解决方案1:



...然后使用运算符'='或'<>来比较结构 System.DateTime 的实例'这意味着同一时刻或不同时刻。

请注意,你也可以使用'<'(早于),'>'(晚于) ,'> ='(不早于),'< ='(不迟于)。



请参阅:http://msdn.microsoft.com/en-us/library/system.datetime%28v= vs.110%29.aspx [ ^ ]。



另外,我不知道你的绑定源是什么,但如果你使用的是数据库,不要将与时间相关的数据存储为字符串数据。出于这些目的,有特殊的数据类型,例如DATE。



-SA
To continue Solution 1:

…and then compare instanced of the structure System.DateTime using the operator '=' or '<>' which means "same moment of time" or "not the same moment of time".
Note that you can also use '<' (sooner than), '>' (later than), '>=' (not sooner than), '<=' (not later than).

Please see: http://msdn.microsoft.com/en-us/library/system.datetime%28v=vs.110%29.aspx[^].

Also, I don't know what is your binding source, but if you use a database, don't store data related to time as string data. For these purposes, there are special data types, such as DATE.

—SA


VB.NET'知道'日期数据类型 [ ^ ]。它的日期为0001年1月1日至9999年12月31日,时间为12:00:00 AM(午夜)至11:59:59.9999999 PM。



示例1 - 使用简单的'='运算符:

VB.NET 'knows' Date data type[^]. It holds dates from January 1 of the year 0001 through December 31 of the year 9999, and times from 12:00:00 AM (midnight) through 11:59:59.9999999 PM.

Example 1 - using simple '=' operator:
Dim a As Date = CDate("2014-09-12"), b As Date = Date.Now

If a = b Then
    Console.WriteLine("Dates are equal {0}={1}", a, b)
Else
    Console.WriteLine("Dates aren't equal {0}<>{1}", a, b)
End If

Console.ReadKey()





例2 - 使用比较方法 [ ^ ]:



Example 2 - using Compare method[^]:

Dim date1 As Date = #08/01/2009 12:00AM#
Dim date2 As Date = #08/01/2009 12:00PM#
Dim result As Integer = DateTime.Compare(date1, date2)
Dim relationship As String

If result < 0 Then
   relationship = "is earlier than"
ElseIf result = 0 Then
   relationship = "is the same time as"
Else
   relationship = "is later than"
End If

Console.WriteLine("{0} {1} {2}", date1, relationship, date2)





例3 - 使用 DateDiff函数 [ ^ ]:



Example 3 - using DateDiff function[^]:

Dim firstDate, msg As String
Dim secondDate As Date
firstDate = InputBox("Enter a date")
Try
    secondDate = CDate(firstDate)
    msg = "Days from today: " & DateDiff(DateInterval.Day, Now, secondDate)
    MsgBox(msg)
Catch
    MsgBox("Not a valid date value.")
End Try







注:正如解释这里:数据类型摘要(Visual Basic) [ ^ ],日期数据类型使用DateTime结构 - 简单地说。




Note: As is explained here: Data Type Summary (Visual Basic)[^], Date data type uses DateTime structure - to put it simply.


这篇关于比较VB.Net中的两个日期?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆