vb.net 仅获取日期

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4861021/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 15:15:46  来源:igfitidea点击:

Getting date only

vb.net

提问by Furqan Sehgal

I want to get today's date. Using Now(), I get both date and time but I need date only. How to get it? Thanks Furqan

我想知道今天的日期。使用 Now(),我可以获得日期和时间,但我只需要日期。如何获得?谢谢富尔坎

回答by stakx - no longer contributing

Use System.DateTime.Today. Some additional notes:

使用System.DateTime.Today. 一些补充说明:

  • While you're only interested in the date, you still get a System.DateTimeobject. That is, there still isa time portion in the returned value, but it's left blank (all zeroes).

    It's important to remember this when you compare dates. If you want to compare two dates, and you've fetched one date via DateTime.Todayand another date via DateTime.Now, then these two dates will not be equal — precisely because one has the time set and the other doesn't.

  • In VB.NET, the Datekeyword is equivalent to the System.DateTimetype. So you can write:

    Date.Today
    

    P.S.:It just occurred to me that it might be a good idea to write Date.Today, but DateTime.Now, in order to make it even more explicit that the former property only returns the date portion, while the latter returns both a date and time portion.

  • 虽然您只对日期感兴趣,但您仍然会得到一个System.DateTime对象。也就是说,仍然在返回值的时间部分,但它的留空(全零)。

    在比较日期时记住这一点很重要。如果您想比较两个日期,并且您已经通过 获取了一个日期DateTime.Today和另一个日期DateTime.Now,那么这两个日期将不相等 — 正是因为其中一个设置了时间而另一个没有。

  • 在 VB.NET 中,Date关键字等效于System.DateTime类型。所以你可以写:

    Date.Today
    

    PS:我突然想到写Date.Today, but可能是个好主意DateTime.Now,为了更明确地说明前一个属性只返回日期部分,而后者同时返回日期和时间部分。

回答by SWeko

Try the DateTime.Todaymethod for just the current date.

尝试DateTime.Today仅针对当前日期的方法。

This returns a DateTimeobject, just like DateTime.Now, but it's time portion is always set to 0.

这将返回一个DateTime对象,就像DateTime.Now,但它的时间部分始终设置为 0。

回答by woodleader

    Imports System

Public Class MainClass

    Shared Sub Main(ByVal args As String())
       Dim currentTime as System.DateTime = System.DateTime.Now
       Console.WriteLine("currentTime Year :" & currentTime.Year)
       Console.WriteLine("currentTime Month:" &  currentTime.Month)
       Console.WriteLine("currentTime Date:" & currentTime.Date)
       Console.WriteLine("currentTime Hour:" & currentTime.Hour)
       Console.WriteLine("currentTime Minute:" & currentTime.Minute)
       Console.WriteLine("currentTime Second:" & currentTime.Second)
    End Sub
End Class

回答by BINU NARAYANAN NELLIYAMPATHI

'try the simple way................... enjoy..  
    Imports System  -general declaration part
Private Sub Show_Click(sender As Object, e As EventArgs) Handles Button1.Click
            Dim currentdate As System.DateTime
            Dim currenttime As System.DateTime
            Dim currentDateTime As System.DateTime
            currentdate = DateAndTime.Today
            currenttime = DateAndTime.TimeOfDay
            currentDateTime = DateAndTime.Now

            MsgBox(currentdate)
            MsgBox(currenttime)
            MsgBox(currentDateTime)
    End Sub

回答by JMaya

Much simpler this way

这种方式简单多了

Public Class Form1
    Dim GDate As String
    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        GDate = Today
    End Sub
End Class

回答by Shailesh Jadyar Ass_star

Whenever you will have to retrieve you will get it and can convert into again date format later also

每当您必须检索时,您都会得到它,并且可以稍后再次转换为日期格式

Dim dt1 As String = Today
Dim time1 As String = TimeOfDay

msgbox(dt1)
msgbox(time1)