DateTime.Now プロパティ
定義
重要
一部の情報は、リリース前に大きく変更される可能性があるプレリリースされた製品に関するものです。 Microsoft は、ここに記載されている情報について、明示または黙示を問わず、一切保証しません。
このコンピューターの現在の日付と時刻に設定され、ローカル時刻として表される DateTime オブジェクトを取得します。
public:
static property DateTime Now { DateTime get(); };
public static DateTime Now { get; }
static member Now : DateTime
Public Shared ReadOnly Property Now As DateTime
プロパティ値
現在のローカルの日付と時刻の値を持つオブジェクト。
例
次の例では、 Now プロパティと UtcNow プロパティを使用して、現在のローカル日時と現在の世界協定 (UTC) の日付と時刻を取得します。 次に、さまざまなカルチャの書式設定規則を使用して、文字列とその Kind プロパティの値を表示します。
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
open System
open System.Globalization
let localDate = DateTime.Now
let utcDate = DateTime.UtcNow
let cultureNames =
[ "en-US"; "en-GB"; "fr-FR"; "de-DE"; "ru-RU" ]
for cultureName in cultureNames do
let culture = CultureInfo cultureName
printfn $"{culture.NativeName}:"
printfn $" Local date and time: {localDate.ToString culture}, {localDate.Kind:G}"
printfn $" UTC date and time: {utcDate.ToString culture}, {utcDate.Kind:G}\n"
// 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値を返します。 0001 年 1 月 1 日の午前 0 時から経過したティック数を表す DateTime 値と、カルチャ固有の形式で日付と時刻の値を表すその DateTime 値の文字列表現には違いがあることに注意してください。 日付と時刻の値の書式設定については、 ToString メソッドを参照してください。 次の例では、短い日付と時刻の文字列をさまざまなカルチャ固有の形式で表示します。
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
open System
open System.Globalization
let localDate = DateTime.Now
let cultureNames =
[ "en-US"; "en-GB"; "fr-FR"; "de-DE"; "ru-RU" ]
for cultureName in cultureNames do
let culture = CultureInfo cultureName
printfn $"{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 以降では、戻り値は、Kind プロパティがDateTimeKind.Localを返すDateTimeです。
注
DateTimeOffset.Now プロパティを使用して、現在のローカルの日付と時刻を取得することもできます。 これにより、ローカル時刻を 1 つの時点として明確に表すことができます。これにより、その時刻の値がコンピューター間で移植可能になります。