DateTimeOffset.Now 属性
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
获取一个 DateTimeOffset 对象,该对象设置为当前计算机上的当前日期和时间,偏移量设置为本地时间与协调世界时 (UTC) 之间的偏移量。
public:
static property DateTimeOffset Now { DateTimeOffset get(); };
public static DateTimeOffset Now { get; }
member this.Now : DateTimeOffset
Public Shared ReadOnly Property Now As DateTimeOffset
属性值
一个 DateTimeOffset 对象,其日期和时间为当前的本地时间,其偏移量为本地时区与协调世界时 (UTC) 之间的偏移量。
示例
以下示例使用Now属性检索当前日期和时间,并使用类型支持DateTimeOffset的每个标准日期和时间格式字符串来显示它。
using System;
public class Example
{
public static void Main()
{
String[] fmtStrings = { "d", "D", "f", "F", "g", "G", "M",
"R", "s", "t", "T", "u", "y" };
DateTimeOffset value = DateTimeOffset.Now;
// Display date in default format.
Console.WriteLine(value);
Console.WriteLine();
// Display date using each of the specified formats.
foreach (var fmtString in fmtStrings)
Console.WriteLine("{0} --> {1}",
fmtString, value.ToString(fmtString));
}
}
// The example displays output similar to the following:
// 11/19/2012 10:57:11 AM -08:00
//
// d --> 11/19/2012
// D --> Monday, November 19, 2012
// f --> Monday, November 19, 2012 10:57 AM
// F --> Monday, November 19, 2012 10:57:11 AM
// g --> 11/19/2012 10:57 AM
// G --> 11/19/2012 10:57:11 AM
// M --> November 19
// R --> Mon, 19 Nov 2012 18:57:11 GMT
// s --> 2012-11-19T10:57:11
// t --> 10:57 AM
// T --> 10:57:11 AM
// u --> 2012-11-19 18:57:11Z
// y --> November, 2012
open System
let fmtStrings =
[ "d"; "D"; "f"; "F"; "g"; "G"; "M"
"R"; "s"; "t"; "T"; "u"; "y" ]
let value = DateTimeOffset.Now
// Display date in default format.
printfn $"{value}\n"
// Display date using each of the specified formats.
for fmtString in fmtStrings do
printfn $"{fmtString} --> {value.ToString fmtString}"
// The example displays output similar to the following:
// 11/19/2012 10:57:11 AM -08:00
//
// d --> 11/19/2012
// D --> Monday, November 19, 2012
// f --> Monday, November 19, 2012 10:57 AM
// F --> Monday, November 19, 2012 10:57:11 AM
// g --> 11/19/2012 10:57 AM
// G --> 11/19/2012 10:57:11 AM
// M --> November 19
// R --> Mon, 19 Nov 2012 18:57:11 GMT
// s --> 2012-11-19T10:57:11
// t --> 10:57 AM
// T --> 10:57:11 AM
// u --> 2012-11-19 18:57:11Z
// y --> November, 2012
Module Example
Public Sub Main()
Dim fmtStrings() As String = { "d", "D", "f", "F", "g", "G",
"M", "R", "s", "t", "T", "u",
"y" }
Dim value As DateTimeOffset = DateTimeOffset.Now
' Display date in default format.
Console.WriteLine(value)
Console.WriteLine()
' Display date using each of the specified formats.
For Each fmtString in fmtStrings
Console.WriteLine("{0} --> {1}",
fmtString, value.ToString(fmtString))
Next
End Sub
End Module
' The example displays output similar to the following:
' 11/19/2012 10:57:11 AM -08:00
'
' d --> 11/19/2012
' D --> Monday, November 19, 2012
' f --> Monday, November 19, 2012 10:57 AM
' F --> Monday, November 19, 2012 10:57:11 AM
' g --> 11/19/2012 10:57 AM
' G --> 11/19/2012 10:57:11 AM
' M --> November 19
' R --> Mon, 19 Nov 2012 18:57:11 GMT
' s --> 2012-11-19T10:57:11
' t --> 10:57 AM
' T --> 10:57:11 AM
' u --> 2012-11-19 18:57:11Z
' y --> November, 2012
以下示例使用 Now 和 Millisecond 属性来确定系统时钟的分辨率。 仅当其毫秒组件的值已更改时,它才会显示时间。
DateTimeOffset dto;
int ctr = 0;
int ms = 0;
do {
dto = DateTimeOffset.Now;
if (dto.Millisecond != ms)
{
ms = dto.Millisecond;
Console.WriteLine("{0}:{1:d3} ms. {2}",
dto.ToString("M/d/yyyy h:mm:ss"),
ms, dto.ToString("zzz"));
ctr++;
}
} while (ctr < 100);
let mutable ms = 0
for _ = 0 to 99 do
let dto = DateTimeOffset.Now
if dto.Millisecond <> ms then
ms <- dto.Millisecond
printfn $"""{dto.ToString "M/d/yyyy h:mm:ss"}:{ms:d3} ms. {dto:zzz}"""
Dim dto As DateTimeOffset
Dim ctr As Integer
Dim ms As Integer
Do
dto = DateTimeOffset.Now
If dto.Millisecond <> ms Then
ms = dto.Millisecond
Console.WriteLine("{0}:{1:d3} ms. {2}", _
dto.ToString("M/d/yyyy h:mm:ss"), _
ms, dto.ToString("zzz"))
ctr += 1
End If
Loop While ctr < 100
注解
当前本地时间毫秒组件的精度取决于系统时钟的分辨率。 在 Windows NT 3.5 和更高版本以及 Windows Vista 操作系统上,时钟的分辨率大约为 10-15 毫秒。