首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

VB.Net - 日期和时间

你写的大部分软件都需要实现某种形式的日期功能,返回当前日期和时间。日期是日常生活的一部分,使用它能让工作变得轻松,不需要太多思考。?VB.Net还提供了强大的日期算术工具,使操作日期变得容易。

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

DateTime 结构表示即时时间,通常表示为日期和时间的一天

代码语言:txt
复制
'Declaration
<SerializableAttribute> _
Public Structure DateTime _
	Implements IComparable, IFormattable, IConvertible, ISerializable,  
	IComparable(Of DateTime), IEquatable(Of DateTime)

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

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

代码语言:txt
复制
'Declaration
<StandardModuleAttribute> _
Public NotInheritable Class DateAndTime

| 注意: DateTime结构和DateAndTime模块都包含诸如Now和Today之类的属性,因此初学者经常会感到困惑。 DateAndTime类属于Microsoft.VisualBasic命名空间,DateTime结构属于System命名空间。因此,使用以后的将帮助你移植你的代码到另一个.Net语言,如C#。 但是,DateAndTime类/模块包含Visual Basic中所有可用的旧日期函数。 |

|:----|

DateTime结构的属性和方法

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

S.N

属性

描述

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.G获取一个值,该值指示此实例表示的时间是基于本地时间,协调世界时间(UTC)还是两者都不是。

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.获取在此计算机上设置为当前日期和时间的DateTime对象,以本地时间表示。

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).获取设置为此计算机上当前日期和时间的DateTime对象,以协调世界时(UTC)表示。

16

Year

Gets the year component of the date represented by this instance.获取此实例表示的日期的年份组件。

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

SN

方法名称和说明

1

Public Function Add (value As TimeSpan) As DateTime公共函数Add(值作为时间跨度)As DateTime 返回一个新的DateTime,将指定的TimeSpan的值添加到此实例的值。

2

Public Function AddDays ( value As Double) As DateTime公共函数AddDays(值为双)As DateTime 返回一个新的DateTime,该值将指定的天数添加到此实例的值中。

3

Public Function AddHours (value As Double) As DateTime公共函数AddHours(值为双)As DateTime 返回一个新的DateTime,该值将指定的小时数添加到此实例的值中。

4

Public Function AddMinutes (value As Double) As DateTime公共函数AddMinutes(值为双)As DateTime 返回一个新的DateTime,将指定的分钟数添加到此实例的值。

5

Public Function AddMonths (months As Integer) As DateTime公共函数AddMonths(月作为整数)As DateTime 返回一个新的DateTime,将指定的月数添加到此实例的值。

6

Public Function AddSeconds (value As Double) As DateTime公共函数AddSeconds(值为双)As DateTime 返回一个新的DateTime,将指定的秒数添加到此实例的值。

7

Public Function AddYears (value As Integer ) As DateTime公共函数AddYears(值为整数)?As DateTime 返回一个新的DateTime,将指定的年数添加到此实例的值。

8

Public Shared Function Compare (t1 As DateTime,t2 As DateTime) As Integer公共共享函数比较(T1作为日期时间,T2为DATETIME)As Integer 比较两个DateTime实例,并返回一个整数,指示第一个实例是早于,与第二个实例相同还是晚于第二个实例。

9

Public Function CompareTo (value As DateTime) As Integer公共函数CompareTo(值为DATETIME)As Integer 将此实例的值与指定的DateTime值进行比较,并返回一个整数,指示此实例是早于,等于还是晚于指定的DateTime值。

10

Public Function Equals (value As DateTime) As Boolean公共函数等于(值为DATETIME)As Boolean 返回一个值,表示此实例的值是否等于指定的DateTime实例的值。

11

Public Shared Function Equals (t1 As DateTime, t2 As DateTime) As Boolean公共共享函数等于(T1作为日期时间,T2为DATETIME)?As Boolean 返回一个值,指示两个DateTime实例是否具有相同的日期和时间值。

12

Public Overrides Function ToString As String公共覆盖函数ToString ?As String将当前DateTime对象的值转换为其等效字符串表示形式。

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

创建DateTime对象

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

  • By calling a DateTime constructor from any of the overloaded DateTime constructors.通过从任何重载的DateTime构造函数调用DateTime构造函数。
  • By assigning the DateTime object a date and time value returned by a property or method. 通过为DateTime对象分配属性或方法返回的日期和时间值。
  • By parsing the string representation of a date and time value. 通过解析日期和时间值的字符串表示。
  • By calling the DateTime structure's implicit default constructor. 通过调用DateTime结构的隐式默认构造函数。

下面的例子说明了这一点:

代码语言:txt
复制
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

当上述代码被编译和执行时,它产生了以下结果:

代码语言:txt
复制
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中的当前日期和时间:

当前时间:

代码语言:txt
复制
Module dateNtime
? ?Sub Main()
? ? ? Console.Write("Current Time: ")
? ? ? Console.WriteLine(Now.ToLongTimeString)
? ? ? Console.ReadKey()
? ?End Sub
End Module

当上述代码被编译和执行时,它产生了以下结果:

代码语言:txt
复制
Current Time: 11 :05 :32 AM

当前日期:

代码语言:txt
复制
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

当上述代码被编译和执行时,它产生了以下结果:

代码语言:txt
复制
Today is: 12/11/2012 12:00:00 AM

格式化日期

日期字面值应该用哈希符号(##)括起来,并以M / d / yyyy格式指定,例如#12/16/2012#。 否则,您的代码可能会更改,具体取决于运行应用程序的语言环境。

例如,您为2012年2月6日的日期指定了#2/6/2012#的日期字面值。对于使用mm / dd / yyyy格式的语言环境,这是正确的。 但是,在使用dd / mm / yyyy格式的语言环境中,您的文本将编译为2012年6月2日。如果语言环境使用另一种格式,例如yyyy / mm / dd,该文字将无效并导致编译器错误。

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

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

代码语言:txt
复制
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

当上述代码被编译和执行时,它产生了以下结果:

代码语言:txt
复制
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函数的样式参数:

格式

描述

General Date, or G

Displays a date and/or time. For example, 1/12/2012 07:07:30 AM显示日期和/或时间。 例如,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.根据您当前区域的长日期格式显示日期。 例如,2012年12月16日星期日

Short Date, or d

Displays a date using your current culture's short date format. For example, 12/12/2012使用当前区域短日期格式显示日期。 例如,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使用您当前区域的长时间格式显示时间; 通常包括小时,分钟,秒。 例如,01:07:30 AM。.

Short Time or t

Displays a time using your current culture's short time format. For example, 11:07 AM使用当前区域的短时格式显示时间。 例如,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根据您当前的区域格式显示长日期和短时间。 例如,2012年12月16日星期日上午12:15。.

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根据您当前的区域格式显示长日期和长时间。 例如,2012年12月16日星期日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根据您当前的区域格式显示短日期和短时间。 例如,12/16/2012 12:15 AM。.

M, m

Displays the month and the day of a date. For example, December 16显示日期的月份和日期。 例如,12月16日。.

R, r

Formats the date according to the RFC1123Pattern property根据RFC1123Pattern属性格式化日期。.

s

Formats the date and time as a sortable index. For example, 2012-12-16T12:07:31将日期和时间格式化为可排序索引。 例如,2012-12-16T12:07:31。.

u

Formats the date and time as a GMT sortable index. For example, 2012-12-16 12:15:31Z将日期和时间格式设置为GMT可排序索引。 例如,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将日期和时间格式设置为长日期和长时间,格式为GMT。 例如,星期日,2012年12月16日下午6:07:31。.

Y, y

Formats the date as the year and month. For example, December, 2012将日期格式设置为年和月。 例如,2012年12月。.

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

属性和DateAndTime类的方法

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

SN

属性

描述

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.返回表示自午夜起经过的秒数为Double值。

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类的常用方法

SN

方法名称和说明

1

Public Shared Function DateAdd (Interval As DateInterval, Number As Double, DateValue As DateTime) As DateTime公共共享函数使用DateAdd(时间间隔为DateInterval,号码为DOUBLE,则DateValue为DATETIME)为DATETIME 返回包含已添加指定时间间隔的日期和时间值的Date值。

2

Public Shared Function DateAdd (Interval As String,Number As Double,DateValue As Object ) As DateTime公共共享函数使用DateAdd(时间间隔为字符串,数字为DOUBLE,则DateValue作为对象)为DATETIME 返回包含已添加指定时间间隔的日期和时间值的Date值。

3

Public Shared Function DateDiff (Interval As DateInterval, Date1 As DateTime, Date2 As DateTime, DayOfWeek As FirstDayOfWeek, WeekOfYear As FirstWeekOfYear ) As Long公共共享函数DateDiff(时间间隔为DateInterval,日期1为DATETIME,日期2为DATETIME,星期作为Firstdayofweek,WEEKOFYEAR作为FirstWeekOfYear)As Long 返回指定两个日期值之间的时间间隔数的长整型值。

4

Public Shared Function DatePart (Interval As DateInterval, DateValue As DateTime, FirstDayOfWeekValue As FirstDayOfWeek, FirstWeekOfYearValue As FirstWeekOfYear ) As Integer公共共享函数DATEPART(时间间隔为DateInterval,则DateValue为DATETIME,FirstDayOfWeekValue作为Firstdayofweek可,FirstWeekOfYearValue作为FirstWeekOfYear)作为整数 返回包含给定Date值的指定组件的整数值。

5

Public Shared Function Day (DateValue As DateTime) As Integer公共共享函数Day(DateValue将作为DATETIME)作为整数 返回从1到31的整数值,表示每月的某一天。

6

Public Shared Function Hour (TimeValue As DateTime) As Integer公共共享函数Hour?(TIMEVALUE为DATETIME)作为整数 返回从0到23的整数值,表示一天中的小时。

7

Public Shared Function Minute (TimeValue As DateTime) As Integer公共共享函数Minute(TIMEVALUE为DATETIME)作为整数 返回一个从0到59的整数值,表示小时的分钟。

8

Public Shared Function Month (DateValue As DateTime) As Integer公共共享函数Month(DateValue将作为DATETIME)作为整数 返回从1到12的整数值,表示一年中的月份。

9

Public Shared Function MonthName (Month As Integer, Abbreviate As Boolean) As String公共共享函数MONTHNAME(月为整数,简称布尔)作为字符串 返回包含指定月份的名称的字符串值。

10

Public Shared Function Second (TimeValue As DateTime) As Integer公共共享函数Second(TIMEVALUE为DATETIME)作为整数 返回从0到59,代表分钟的第二个整数值。

11

Public Overridable Function ToString As String公众可重写的ToString函数作为字符串 返回表示当前对象的字符串。

12

Public Shared Function Weekday (DateValue As DateTime, DayOfWeek As FirstDayOfWeek) As Integer公共共享函数Weekday?(DateValue将作为日期时间,星期由于Firstdayofweek可)作为整数 返回包含表示星期几的一个数的整数值。

13

Public Shared Function WeekdayName (Weekday As Integer, Abbreviate As Boolean, FirstDayOfWeekValue As FirstDayOfWeek) As String公共共享函数WEEKDAYNAME(平日为整数,简称布尔,FirstDayOfWeekValue作为Firstdayofweek可)作为字符串 返回包含指定工作日名称的字符串值。

14

Public Shared Function Year (DateValue As DateTime) As Integer公共共享函数(一年则DateValue为DATETIME)作为整数 返回从1到9999表示年份的整数值。

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

下面的程序演示其中的一些方法:

代码语言:txt
复制
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

当上述代码被编译和执行时,它产生了以下结果:

代码语言:txt
复制
7/27/1998 12:00:00 AM
27
7
July

扫码关注腾讯云开发者

领取腾讯云代金券

http://www.vxiaotou.com