📜  VB.Net-日期和时间

📅  最后修改于: 2020-11-19 08:52:13             🧑  作者: Mango


您编写的大多数软件都需要实现某种形式的日期函数,以返回当前日期和时间。日期是日常生活的重要组成部分,以至于不加思索就可以轻松地与他们合作。 VB.Net还提供了强大的日期算术工具,使日期操作变得容易。

日期数据类型包含日期值,时间值或日期和时间值。 Date的默认值为0001年1月1日的0:00:00(午夜)。等效的.NET数据类型为System.DateTime

DateTime结构表示时间的瞬间,通常表示为日期和时间

'Declaration
 _
Public Structure DateTime _
   Implements IComparable, IFormattable, IConvertible, ISerializable,  
   IComparable(Of DateTime), IEquatable(Of DateTime)

您还可以从DateAndTime类获取当前日期和时间。

DateAndTime模块包含在日期和时间操作中使用的过程和属性。

'Declaration
 _
Public NotInheritable Class DateAndTime

Note:

Both the DateTime structure and the DateAndTime module contain properties like Now and Today, so often beginners find it confusing. The DateAndTime class belongs to the Microsoft.VisualBasic namespace and the DateTime structure belongs to the System namespace.
Therefore, using the later would help you in porting your code to another .Net language like C#. However, the DateAndTime class/module contains all the legacy date functions available in Visual Basic.

DateTime结构的属性和方法

下表列出了DateTime结构的一些常用属性

Sr.No Property Description
1 Date Gets the date component of this instance.
2 Day Gets the day of the month represented by this instance.
3 DayOfWeek Gets the day of the week represented by this instance.
4 DayOfYear Gets the day of the year represented by this instance.
5 Hour Gets the hour component of the date represented by this instance.
6 Kind Gets a value that indicates whether the time represented by this instance is based on local time, Coordinated Universal Time (UTC), or neither.
7 Millisecond Gets the milliseconds component of the date represented by this instance.
8 Minute Gets the minute component of the date represented by this instance.
9 Month Gets the month component of the date represented by this instance.
10 Now Gets a DateTime object that is set to the current date and time on this computer, expressed as the local time.
11 Second Gets the seconds component of the date represented by this instance.
12 Ticks Gets the number of ticks that represent the date and time of this instance.
13 TimeOfDay Gets the time of day for this instance.
14 Today Gets the current date.
15 UtcNow Gets a DateTime object that is set to the current date and time on this computer, expressed as the Coordinated Universal Time (UTC).
16 Year Gets the year component of the date represented by this instance.

下表列出了DateTime结构的一些常用方法

Sr.No Method Name & Description
1

Public Function Add (value As TimeSpan) As DateTime

Returns a new DateTime that adds the value of the specified TimeSpan to the value of this instance.

2

Public Function AddDays ( value As Double) As DateTime

Returns a new DateTime that adds the specified number of days to the value of this instance.

3

Public Function AddHours (value As Double) As DateTime

Returns a new DateTime that adds the specified number of hours to the value of this instance.

4

Public Function AddMinutes (value As Double) As DateTime

Returns a new DateTime that adds the specified number of minutes to the value of this instance.

5

Public Function AddMonths (months As Integer) As DateTime

Returns a new DateTime that adds the specified number of months to the value of this instance.

6

Public Function AddSeconds (value As Double) As DateTime

Returns a new DateTime that adds the specified number of seconds to the value of this instance.

7

Public Function AddYears (value As Integer ) As DateTime

Returns a new DateTime that adds the specified number of years to the value of this instance.

8

Public Shared Function Compare (t1 As DateTime,t2 As DateTime) As Integer

Compares two instances of DateTime and returns an integer that indicates whether the first instance is earlier than, the same as, or later than the second instance.

9

Public Function CompareTo (value As DateTime) As Integer

Compares the value of this instance to a specified DateTime value and returns an integer that indicates whether this instance is earlier than, the same as, or later than the specified DateTime value.

10

Public Function Equals (value As DateTime) As Boolean

Returns a value indicating whether the value of this instance is equal to the value of the specified DateTime instance.

11

Public Shared Function Equals (t1 As DateTime, t2 As DateTime) As Boolean

Returns a value indicating whether two DateTime instances have the same date and time value.

12

Public Overrides Function ToString As String

Converts the value of the current DateTime object to its equivalent string representation.

上面的方法列表并不详尽,请访问Microsoft文档以获取DateTime结构的方法和属性的完整列表。

创建一个DateTime对象

您可以通过以下方式之一创建DateTime对象-

  • 通过从任何重载的DateTime构造函数中调用DateTime构造函数。

  • 通过为DateTime对象分配由属性或方法返回的日期和时间值。

  • 通过解析日期和时间值的字符串表示形式。

  • 通过调用DateTime结构的隐式默认构造函数。

以下示例演示了这一点-

Module Module1
   Sub Main()
      'DateTime constructor: parameters year, month, day, hour, min, sec
      Dim date1 As New Date(2012, 12, 16, 12, 0, 0)
      'initializes a new DateTime value
      Dim date2 As Date = #12/16/2012 12:00:52 AM#
      'using properties
      Dim date3 As Date = Date.Now
      Dim date4 As Date = Date.UtcNow
      Dim date5 As Date = Date.Today
      
      Console.WriteLine(date1)
      Console.WriteLine(date2)
      Console.WriteLine(date3)
      Console.WriteLine(date4)
      Console.WriteLine(date5)
      Console.ReadKey()
   End Sub
End Module

编译并执行上述代码后,将产生以下结果-

12/16/2012 12:00:00 PM
12/16/2012 12:00:52 PM
12/12/2012 10:22:50 PM
12/12/2012 12:00:00 PM

获取当前日期和时间

以下程序演示了如何在VB.Net中获取当前日期和时间-

当前时间-

Module dateNtime
   Sub Main()
      Console.Write("Current Time: ")
      Console.WriteLine(Now.ToLongTimeString)
      Console.ReadKey()
   End Sub
End Module

编译并执行上述代码后,将产生以下结果-

Current Time: 11 :05 :32 AM

当前日期-

Module dateNtime
   Sub Main()
      Console.WriteLine("Current Date: ")
      Dim dt As Date = Today
      Console.WriteLine("Today is: {0}", dt)
      Console.ReadKey()
   End Sub
End Module

编译并执行上述代码后,将产生以下结果-

Today is: 12/11/2012 12:00:00 AM

格式化日期

Date字面量应包含在井号(##)中,并以M / d / yyyy格式指定,例如#12/16/2012#。否则,您的代码可能会根据运行应用程序的区域设置而改变。

例如,您将2012年2月6日指定为#2/6/2012#的Date字面量。对于使用mm / dd / yyyy格式的语言环境来说,这是正确的。但是,在使用dd / mm / yyyy格式的语言环境中,您的字面量将编译为2012年6月2日。如果在区域设置中使用另一种格式,例如yyyy / mm / dd,则该字面量将无效并导致编译器错误。

要将Date字面量转换为您的语言环境格式或自定义格式,请使用String类的Format函数,指定预定义或用户定义的日期格式。

下面的示例演示了这一点。

Module dateNtime
   Sub Main()
      Console.WriteLine("India Wins Freedom: ")
      Dim independenceDay As New Date(1947, 8, 15, 0, 0, 0)
      ' Use format specifiers to control the date display.
      Console.WriteLine(" Format 'd:' " & independenceDay.ToString("d"))
      Console.WriteLine(" Format 'D:' " & independenceDay.ToString("D"))
      Console.WriteLine(" Format 't:' " & independenceDay.ToString("t"))
      Console.WriteLine(" Format 'T:' " & independenceDay.ToString("T"))
      Console.WriteLine(" Format 'f:' " & independenceDay.ToString("f"))
      Console.WriteLine(" Format 'F:' " & independenceDay.ToString("F"))
      Console.WriteLine(" Format 'g:' " & independenceDay.ToString("g"))
      Console.WriteLine(" Format 'G:' " & independenceDay.ToString("G"))
      Console.WriteLine(" Format 'M:' " & independenceDay.ToString("M"))
      Console.WriteLine(" Format 'R:' " & independenceDay.ToString("R"))
      Console.WriteLine(" Format 'y:' " & independenceDay.ToString("y"))
      Console.ReadKey()
   End Sub
End Module

编译并执行上述代码后,将产生以下结果-

India Wins Freedom:
Format 'd:' 8/15/1947
Format 'D:' Friday, August 15, 1947
Format 't:' 12:00 AM
Format 'T:' 12:00:00 AM
Format 'f:' Friday, August 15, 1947 12:00 AM
Format 'F:' Friday, August 15, 1947 12:00:00 AM
Format 'g:' 8/15/1947 12:00 AM
Format 'G:' 8/15/1947 12:00:00 AM
Format 'M:' 8/15/1947 August 15
Format 'R:' Fri, 15 August 1947 00:00:00 GMT
Format 'y:' August, 1947

预定义的日期/时间格式

下表列出了预定义的日期和时间格式名称。这些可以按名称用作Format函数的样式参数-

Format Description
General Date, or G Displays a date and/or time. For example, 1/12/2012 07:07:30 AM.
Long Date,Medium Date, or D Displays a date according to your current culture’s long date format. For example, Sunday, December 16, 2012.
Short Date, or d Displays a date using your current culture’s short date format. For example, 12/12/2012.
Long Time,Medium Time, orT Displays a time using your current culture’s long time format; typically includes hours, minutes, seconds. For example, 01:07:30 AM.
Short Time or t Displays a time using your current culture’s short time format. For example, 11:07 AM.
f Displays the long date and short time according to your current culture’s format. For example, Sunday, December 16, 2012 12:15 AM.
F Displays the long date and long time according to your current culture’s format. For example, Sunday, December 16, 2012 12:15:31 AM.
g Displays the short date and short time according to your current culture’s format. For example, 12/16/2012 12:15 AM.
M, m Displays the month and the day of a date. For example, December 16.
R, r Formats the date according to the RFC1123Pattern property.
s Formats the date and time as a sortable index. For example, 2012-12-16T12:07:31.
u Formats the date and time as a GMT sortable index. For example, 2012-12-16 12:15:31Z.
U Formats the date and time with the long date and long time as GMT. For example, Sunday, December 16, 2012 6:07:31 PM.
Y, y Formats the date as the year and month. For example, December, 2012.

对于其他格式,例如用户定义的格式,请查阅Microsoft文档

DateAndTime类的属性和方法

下表列出了DateAndTime类的一些常用属性

Sr.No Property & Description
1

Date

Returns or sets a String value representing the current date according to your system.

2

Now

Returns a Date value containing the current date and time according to your system.

3

TimeOfDay

Returns or sets a Date value containing the current time of day according to your system.

4

Timer

Returns a Double value representing the number of seconds elapsed since midnight.

5

TimeString

Returns or sets a String value representing the current time of day according to your system.

6

Today

Gets the current date.

下表列出了DateAndTime类的一些常用方法

Sr.No Method Name & Description
1

Public Shared Function DateAdd (Interval As DateInterval, Number As Double, DateValue As DateTime) As DateTime

Returns a Date value containing a date and time value to which a specified time interval has been added.

2

Public Shared Function DateAdd (Interval As String,Number As Double,DateValue As Object ) As DateTime

Returns a Date value containing a date and time value to which a specified time interval has been added.

3

Public Shared Function DateDiff (Interval As DateInterval, Date1 As DateTime, Date2 As DateTime, DayOfWeek As FirstDayOfWeek, WeekOfYear As FirstWeekOfYear ) As Long

Returns a Long value specifying the number of time intervals between two Date values.

4

Public Shared Function DatePart (Interval As DateInterval, DateValue As DateTime, FirstDayOfWeekValue As FirstDayOfWeek, FirstWeekOfYearValue As FirstWeekOfYear ) As Integer

Returns an Integer value containing the specified component of a given Date value.

5

Public Shared Function Day (DateValue As DateTime) As Integer

Returns an Integer value from 1 through 31 representing the day of the month.

6

Public Shared Function Hour (TimeValue As DateTime) As Integer

Returns an Integer value from 0 through 23 representing the hour of the day.

7

Public Shared Function Minute (TimeValue As DateTime) As Integer

Returns an Integer value from 0 through 59 representing the minute of the hour.

8

Public Shared Function Month (DateValue As DateTime) As Integer

Returns an Integer value from 1 through 12 representing the month of the year.

9

Public Shared Function MonthName (Month As Integer, Abbreviate As Boolean) As String

Returns a String value containing the name of the specified month.

10

Public Shared Function Second (TimeValue As DateTime) As Integer

Returns an Integer value from 0 through 59 representing the second of the minute.

11

Public Overridable Function ToString As String

Returns a string that represents the current object.

12

Public Shared Function Weekday (DateValue As DateTime, DayOfWeek As FirstDayOfWeek) As Integer

Returns an Integer value containing a number representing the day of the week.

13

Public Shared Function WeekdayName (Weekday As Integer, Abbreviate As Boolean, FirstDayOfWeekValue As FirstDayOfWeek) As String

Returns a String value containing the name of the specified weekday.

14

Public Shared Function Year (DateValue As DateTime) As Integer

Returns an Integer value from 1 through 9999 representing the year.

上面的列表并不详尽。有关DateAndTime类的属性和方法的完整列表,请查阅Microsoft文档

以下程序演示了其中的一些方法-

Module Module1
   Sub Main()
      Dim birthday As Date
      Dim bday As Integer
      Dim month As Integer
      Dim monthname As String
      ' Assign a date using standard short format.
      birthday = #7/27/1998#
      bday = Microsoft.VisualBasic.DateAndTime.Day(birthday)
      month = Microsoft.VisualBasic.DateAndTime.Month(birthday)
      monthname = Microsoft.VisualBasic.DateAndTime.MonthName(month)
      
      Console.WriteLine(birthday)
      Console.WriteLine(bday)
      Console.WriteLine(month)
      Console.WriteLine(monthname)
      Console.ReadKey()
   End Sub
End Module

编译并执行上述代码后,将产生以下结果-

7/27/1998 12:00:00 AM
27
7
July