DateTime.Now 屬性
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
取得 DateTime 物件,此物件會設定為這部電腦上目前的日期和時間,以本地時間表示。
public:
static property DateTime Now { DateTime get(); };
public static DateTime Now { get; }
member this.Now : DateTime
Public Shared ReadOnly Property Now As DateTime
屬性值
物件,其值為目前的本地日期和時間。
範例
下列範例會使用 Now 和 UtcNow 屬性,以取得目前的本機日期和時間,以及目前的全球協調 (UTC) 日期和時間。 然後,它會使用許多文化特性的格式化慣例來顯示字串,以及它們的 Kind 屬性值。
using namespace System;
using namespace System::Globalization;
void main()
{
DateTime localDate = DateTime::Now;
DateTime utcDate = DateTime::UtcNow;
array<String^>^ cultureNames = { "en-US", "en-GB", "fr-FR",
"de-DE", "ru-RU" } ;
for each (String^ cultureName in cultureNames) {
CultureInfo^ culture = gcnew CultureInfo(cultureName);
Console::WriteLine("{0}:", culture->NativeName);
Console::WriteLine(" Local date and time: {0}, {1:G}",
localDate.ToString(culture), localDate.Kind);
Console::WriteLine(" UTC date and time: {0}, {1:G}\n",
utcDate.ToString(culture), utcDate.Kind);
}
}
// The example displays the following output:
// English (United States):
// Local date and time: 6/19/2015 10:35:50 AM, Local
// UTC date and time: 6/19/2015 5:35:50 PM, Utc
//
// English (United Kingdom):
// Local date and time: 19/06/2015 10:35:50, Local
// UTC date and time: 19/06/2015 17:35:50, Utc
//
// français (France):
// Local date and time: 19/06/2015 10:35:50, Local
// UTC date and time: 19/06/2015 17:35:50, Utc
//
// Deutsch (Deutschland):
// Local date and time: 19.06.2015 10:35:50, Local
// UTC date and time: 19.06.2015 17:35:50, Utc
//
// русский (Россия):
// Local date and time: 19.06.2015 10:35:50, Local
// UTC date and time: 19.06.2015 17:35:50, Utc
using System;
using System.Globalization;
public class Example
{
public static void Main()
{
DateTime localDate = DateTime.Now;
DateTime utcDate = DateTime.UtcNow;
String[] cultureNames = { "en-US", "en-GB", "fr-FR",
"de-DE", "ru-RU" } ;
foreach (var cultureName in cultureNames) {
var culture = new CultureInfo(cultureName);
Console.WriteLine("{0}:", culture.NativeName);
Console.WriteLine(" Local date and time: {0}, {1:G}",
localDate.ToString(culture), localDate.Kind);
Console.WriteLine(" UTC date and time: {0}, {1:G}\n",
utcDate.ToString(culture), utcDate.Kind);
}
}
}
// The example displays the following output:
// English (United States):
// Local date and time: 6/19/2015 10:35:50 AM, Local
// UTC date and time: 6/19/2015 5:35:50 PM, Utc
//
// English (United Kingdom):
// Local date and time: 19/06/2015 10:35:50, Local
// UTC date and time: 19/06/2015 17:35:50, Utc
//
// français (France):
// Local date and time: 19/06/2015 10:35:50, Local
// UTC date and time: 19/06/2015 17:35:50, Utc
//
// Deutsch (Deutschland):
// Local date and time: 19.06.2015 10:35:50, Local
// UTC date and time: 19.06.2015 17:35:50, Utc
//
// русский (Россия):
// Local date and time: 19.06.2015 10:35:50, Local
// UTC date and time: 19.06.2015 17:35:50, Utc
Imports System.Globalization
Module Example
Public Sub Main()
Dim localDate = DateTime.Now
Dim utcDate = DateTime.UtcNow
Dim cultureNames() As String = { "en-US", "en-GB", "fr-FR",
"de-DE", "ru-RU" }
For Each cultureName In cultureNames
Dim culture As New CultureInfo(cultureName)
Console.WriteLine("{0}:", culture.NativeName)
Console.WriteLine(" Local date and time: {0}, {1:G}",
localDate.ToString(culture), localDate.Kind)
Console.WriteLine(" UTC date and time: {0}, {1:G}",
utcDate.ToString(culture), utcDate.Kind)
Console.WriteLine()
Next
End Sub
End Module
' The example displays the following output:
' English (United States):
' Local date and time: 6/19/2015 10:35:50 AM, Local
' UTC date and time: 6/19/2015 5:35:50 PM, Utc
'
' English (United Kingdom):
' Local date and time: 19/06/2015 10:35:50, Local
' UTC date and time: 19/06/2015 17:35:50, Utc
'
' français (France):
' Local date and time: 19/06/2015 10:35:50, Local
' UTC date and time: 19/06/2015 17:35:50, Utc
'
' Deutsch (Deutschland):
' Local date and time: 19.06.2015 10:35:50, Local
' UTC date and time: 19.06.2015 17:35:50, Utc
'
' русский (Россия):
' Local date and time: 19.06.2015 10:35:50, Local
' UTC date and time: 19.06.2015 17:35:50, Utc
備註
Now屬性 DateTime 會傳回值,表示本機電腦上目前的日期和時間。 請注意,值之間有差異 DateTime ,表示自0001年1月1日午夜起經過的刻度數目,以及該值的字串表示,這會以特定文化特性的格式來表示 DateTime 日期和時間值。 如需格式化日期和時間值的詳細資訊,請參閱 ToString 方法。 下列範例會顯示多個特定文化特性格式的簡短日期和時間字串。
using namespace System;
using namespace System::Globalization;
void main()
{
DateTime localDate = DateTime::Now;
array<String^>^ cultureNames = { "en-US", "en-GB", "fr-FR",
"de-DE", "ru-RU" };
for each (String^ cultureName in cultureNames) {
CultureInfo^ culture = gcnew CultureInfo(cultureName);
Console::WriteLine("{0}: {1}", cultureName,
localDate.ToString(culture));
}
}
// The example displays the following output:
// en-US: 6/19/2015 10:03:06 AM
// en-GB: 19/06/2015 10:03:06
// fr-FR: 19/06/2015 10:03:06
// de-DE: 19.06.2015 10:03:06
// ru-RU: 19.06.2015 10:03:06
using System;
using System.Globalization;
public class Example
{
public static void Main()
{
DateTime localDate = DateTime.Now;
String[] cultureNames = { "en-US", "en-GB", "fr-FR",
"de-DE", "ru-RU" };
foreach (var cultureName in cultureNames) {
var culture = new CultureInfo(cultureName);
Console.WriteLine("{0}: {1}", cultureName,
localDate.ToString(culture));
}
}
}
// The example displays the following output:
// en-US: 6/19/2015 10:03:06 AM
// en-GB: 19/06/2015 10:03:06
// fr-FR: 19/06/2015 10:03:06
// de-DE: 19.06.2015 10:03:06
// ru-RU: 19.06.2015 10:03:06
Imports System.Globalization
Module Example
Public Sub Main()
Dim localDate = DateTime.Now
Dim cultureNames() As String = { "en-US", "en-GB", "fr-FR",
"de-DE", "ru-RU" }
For Each cultureName In cultureNames
Dim culture As New CultureInfo(cultureName)
Console.WriteLine("{0}: {1}", cultureName,
localDate.ToString(culture))
Next
End Sub
End Module
' The example displays the following output:
' en-US: 6/19/2015 10:03:06 AM
' en-GB: 19/06/2015 10:03:06
' fr-FR: 19/06/2015 10:03:06
' de-DE: 19.06.2015 10:03:06
' ru-RU: 19.06.2015 10:03:06
這個屬性的解決方式取決於系統計時器,這取決於基礎作業系統。 它通常會介於0.5 到15毫秒之間。 因此, Now 在短時間間隔內(例如在迴圈中)重複呼叫屬性,可能會傳回相同的值。
Now屬性經常用來測量效能。 不過,由於其低解析度,因此不適合作為效能評定工具使用。 更好的替代方式是使用 Stopwatch 類別。
從 .NET Framework 版本2.0 開始,傳回值是其屬性會傳回的 DateTime Kind DateTimeKind.Local 。
注意
您也可以使用 DateTimeOffset.Now 屬性來取得目前的本機日期和時間。 這可讓本地時間以明確的方式表示為單一時間點,進而讓該時間值可在電腦之間移植。