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 毫秒。