当前位置:K88软件开发文章中心编程语言.NET.NET02 → 文章内容

VB.Net - 日期和时间

减小字体 增大字体 作者:佚名  来源:网上搜集  发布时间:2019-1-15 15:58:59

的值转换为其等效字符串表示形式。以上列出的方法并不详尽,请访问微软的文档以获取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结构的隐式默认构造函数。 下面的例子说明了这一点: 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 SubEnd Module当上述代码被编译和执行时,它产生了以下结果: 12/16/2012 12:00:00 PM12/16/2012 12:00:52 PM12/12/2012 10:22:50 PM12/12/2012 12:00:00 PM获取当前日期和时间: 以下程序演示如何获取VB.Net中的当前日期和时间: 当前时间: Module dateNtime   Sub Main()      Console.Write("Current Time: ")      Console.WriteLine(Now.ToLongTimeString)      Console.ReadKey()   End SubEnd 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 SubEnd Module当上述代码被编译和执行时,它产生了以下结果: 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函数,指定预定义或用户定义的日期格式。下面的例子演示了这一点。 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 SubEnd Module当上述代码被编译和执行时,它产生了以下结果: India Wins Freedom:Format 'd:' 8/15/1947Format 'D:' Friday, August 15, 1947Format 't:' 12:00 AMFormat 'T:' 12:00:00 AMFormat 'f:' Friday, August 15, 1947 12:00 AMFormat 'F:' Friday, August 15, 1947 12:00:00 AMFormat 'g:' 8/15/1947 12:00 AMFormat 'G:' 8/15/1947 12:00:00 AMFormat 'M:' 8/15/1947 August 15Format 'R:' Fri, 15 August 1947 00:00:00 GMTFormat 'y:' August, 1947预定义的日期/时间格式下表列出了预定义的日期和时间格式名称。 可以通过这些名称用作Format函数的样式参数:格式描述General Date, or GDisplays 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 DDisplays a date according to your current culture's long date format. For example, Sunday, December 16,2012.根据您当前区域的长日期格式显示日期。 例如,2012年12月16日星期日Short Date, or dDisplays a date using your current culture's short date format. For example, 12/12/2012使用当前区域短日期格式显示日期。 例如,12/12/2012。.Long Time,Medium Time, orTDisplays 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 tDisplays a time using your current culture's short time format. For example, 11:07 AM使用当前区域的短时格式显示时间。 例如,11:07 AM。.fDisplays the long date and short time according to your current culture's format. For example, Sunday, December 16, 2012 12:15 AM根据您当前的区域格式显示长日期和短时间。 例如,2012年12月

上一页  [1] [2] [3] [4]  下一页


VB.Net - 日期和时间