英語で読む

次の方法で共有


DateTime コンストラクター

定義

DateTime 構造体の新しいインスタンスを初期化します。

オーバーロード

DateTime(Int64)

DateTime 構造体の新しいインスタンスを、指定したタイマー刻み数に初期化します。

DateTime(Int32, Int32, Int32, Int32, Int32, Int32, Int32, Int32, Calendar)

DateTime 構造体の新しいインスタンスを、指定した暦の指定した年、月、日、時、分、秒、ミリ秒、および世界協定時刻 (UTC) または現地時刻に初期化します。

DateTime(Int32, Int32, Int32, Int32, Int32, Int32, Int32, Int32, DateTimeKind)

DateTime 構造体の新しいインスタンスを、指定した暦の指定した年、月、日、時、分、秒、ミリ秒、および世界協定時刻 (UTC) または現地時刻に初期化します。

DateTime(Int32, Int32, Int32, Int32, Int32, Int32, Int32, Calendar, DateTimeKind)

DateTime 構造体の新しいインスタンスを、指定した暦の指定した年、月、日、時、分、秒、ミリ秒、および世界協定時刻 (UTC) または現地時刻に初期化します。

DateTime(Int32, Int32, Int32, Int32, Int32, Int32, Int32, Int32)

DateTime 構造体の新しいインスタンスを、指定した暦の指定した年、月、日、時、分、秒、ミリ秒、および世界協定時刻 (UTC) または現地時刻に初期化します。

DateTime(Int32, Int32, Int32, Int32, Int32, Int32, Int32, Calendar)

DateTime 構造体の新しいインスタンスを、指定した暦の指定した年、月、日、時、分、秒、およびミリ秒に初期化します。

DateTime(Int32, Int32, Int32, Int32, Int32, Int32, Int32, DateTimeKind)

DateTime 構造体の新しいインスタンスを、指定した年、月、日、時、分、秒、ミリ秒、および世界協定時刻 (UTC) または現地時刻に初期化します。

DateTime(Int32, Int32, Int32, Int32, Int32, Int32, Int32)

DateTime 構造体の新しいインスタンスを、指定した年、月、日、時、分、秒、およびミリ秒に初期化します。

DateTime(Int32, Int32, Int32, Int32, Int32, Int32, Int32, Int32, Calendar, DateTimeKind)

DateTime 構造体の新しいインスタンスを、指定した暦の指定した年、月、日、時、分、秒、ミリ秒、および世界協定時刻 (UTC) または現地時刻に初期化します。

DateTime(Int32, Int32, Int32, Int32, Int32, Int32, DateTimeKind)

DateTime 構造体の新しいインスタンスを、指定した年、月、日、時、分、秒、および世界協定時刻 (UTC) または現地時刻に初期化します。

DateTime(Int32, Int32, Int32, Int32, Int32, Int32)

DateTime 構造体の新しいインスタンスを、指定した年、月、日、時、分、および秒に初期化します。

DateTime(Int32, Int32, Int32, Calendar)

DateTime 構造体の新しいインスタンスを、指定した暦の指定した年、月、および日に初期化します。

DateTime(Int32, Int32, Int32)

DateTime 構造体の新しいインスタンスを、指定した年、月、日に初期化します。

DateTime(DateOnly, TimeOnly, DateTimeKind)

指定した に構造体の新しいインスタンスをDateTime初期化し、TimeOnly指定した DateOnly を尊重しますDateTimeKind

DateTime(Int64, DateTimeKind)

DateTime 構造体の新しいインスタンスを、指定したタイマー刻み数と世界協定時刻 (UTC) または現地時刻に初期化します。

DateTime(DateOnly, TimeOnly)

指定した DateOnly および TimeOnlyに構造体のDateTime新しいインスタンスを初期化します。 新しいインスタンスには、 の種類があります Unspecified

DateTime(Int32, Int32, Int32, Int32, Int32, Int32, Calendar)

DateTime 構造体の新しいインスタンスを、指定した暦の指定した年、月、日、時、分、および秒に初期化します。

DateTime(Int64)

ソース:
DateTime.cs
ソース:
DateTime.cs
ソース:
DateTime.cs

DateTime 構造体の新しいインスタンスを、指定したタイマー刻み数に初期化します。

public DateTime (long ticks);

パラメーター

ticks
Int64

日時をグレゴリオ暦の 0001 年 1 月 1 日の 00:00:00.000 以降の経過時間 (100 ナノ秒単位) で表した値。

例外

ticksDateTime.MinValue より小さいか、 DateTime.MaxValue より大きい。

次の例では、コンストラクターの 1 つを DateTime 示します。

// This example demonstrates the DateTime(Int64) constructor.
using System;
using System.Globalization;

class Sample
{
    public static void Main()
    {
// Instead of using the implicit, default "G" date and time format string, we
// use a custom format string that aligns the results and inserts leading zeroes.
    string format = "{0}) The {1} date and time is {2:MM/dd/yyyy hh:mm:ss tt}";

// Create a DateTime for the maximum date and time using ticks.
    DateTime dt1 = new DateTime(DateTime.MaxValue.Ticks);

// Create a DateTime for the minimum date and time using ticks.
    DateTime dt2 = new DateTime(DateTime.MinValue.Ticks);

// Create a custom DateTime for 7/28/1979 at 10:35:05 PM using a
// calendar based on the "en-US" culture, and ticks.
    long ticks = new DateTime(1979, 07, 28, 22, 35, 5,
    new CultureInfo("en-US", false).Calendar).Ticks;
    DateTime dt3 = new DateTime(ticks);

    Console.WriteLine(format, 1, "maximum", dt1);
    Console.WriteLine(format, 2, "minimum", dt2);
    Console.WriteLine(format, 3, "custom ", dt3);
    Console.WriteLine("\nThe custom date and time is created from {0:N0} ticks.", ticks);
    }
}
/*
This example produces the following results:

1) The maximum date and time is 12/31/9999 11:59:59 PM
2) The minimum date and time is 01/01/0001 12:00:00 AM
3) The custom  date and time is 07/28/1979 10:35:05 PM

The custom date and time is created from 624,376,461,050,000,000 ticks.

*/

注釈

Kind プロパティは Unspecified に初期化されます。

日付と時刻のデータの移植性やタイム ゾーンの認識の程度が限られているアプリケーションの場合は、対応する DateTimeOffset コンストラクターを使用できます。

適用対象

.NET 9 およびその他のバージョン
製品 バージョン
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1
UWP 10.0

DateTime(Int32, Int32, Int32, Int32, Int32, Int32, Int32, Int32, Calendar)

ソース:
DateTime.cs
ソース:
DateTime.cs
ソース:
DateTime.cs

DateTime 構造体の新しいインスタンスを、指定した暦の指定した年、月、日、時、分、秒、ミリ秒、および世界協定時刻 (UTC) または現地時刻に初期化します。

public DateTime (int year, int month, int day, int hour, int minute, int second, int millisecond, int microsecond, System.Globalization.Calendar calendar);

パラメーター

year
Int32

年 (1 から calendar の年数)。

month
Int32

月 (1 から calendar の月数)。

day
Int32

日付 (1 ~ month の日数)。

hour
Int32

時間 (0 から 23)。

minute
Int32

分 (0 から 59)。

second
Int32

秒 (0 から 59)。

millisecond
Int32

ミリ秒 (0 から 999)。

microsecond
Int32

マイクロ秒 (0 から 999)。

calendar
Calendar

yearmonth、および day を解釈するために使用される暦。

例外

calendarnull です

yearcalendar でサポートされる範囲にありません。

または

month は 1 未満か、calendar の月数を超えています。

または

day が 1 未満であるか、または monthの日数を超えています。

または

hour が 0 未満であるか、または 23 を超えています。

または

minute が 0 未満であるか、または 59 を超えています。

または

second が 0 未満であるか、または 59 を超えています。

または

millisecond が 0 未満であるか、または 999 を超えています。

- または -

microsecond が 0 未満であるか、または 999 を超えています。

注釈

、、および day パラメーターに使用monthyearできる値は、 パラメーターによって異なりますcalendar。 指定した日付と時刻を を使用して calendar表現できない場合、例外がスローされます。

日付と時刻のデータの移植性やタイム ゾーンの認識の程度が限られているアプリケーションの場合は、対応する DateTimeOffset コンストラクターを使用できます。

適用対象

.NET 9 およびその他のバージョン
製品 バージョン
.NET 7, 8, 9

DateTime(Int32, Int32, Int32, Int32, Int32, Int32, Int32, Int32, DateTimeKind)

ソース:
DateTime.cs
ソース:
DateTime.cs
ソース:
DateTime.cs

DateTime 構造体の新しいインスタンスを、指定した暦の指定した年、月、日、時、分、秒、ミリ秒、および世界協定時刻 (UTC) または現地時刻に初期化します。

public DateTime (int year, int month, int day, int hour, int minute, int second, int millisecond, int microsecond, DateTimeKind kind);

パラメーター

year
Int32

年 (1 から 9999)。

month
Int32

月 (1 ~ 12)。

day
Int32

日付 (1 ~ month の日数)。

hour
Int32

時間 (0 から 23)。

minute
Int32

分 (0 から 59)。

second
Int32

秒 (0 から 59)。

millisecond
Int32

ミリ秒 (0 から 999)。

microsecond
Int32

マイクロ秒 (0 から 999)。

kind
DateTimeKind

yearmonthdayhourminutesecond、および millisecond が、現地時刻、世界協定時刻 (UTC)、またはそのどちらでもない時刻のうち、どれを指定するかを示す列挙値の 1 つ。

例外

year が 1 未満であるか、または 9999 を超えています。

または

month が 1 未満であるか、または 12 を超えています。

または

day が 1 未満であるか、または monthの日数を超えています。

または

hour が 0 未満であるか、または 23 を超えています。

または

minute が 0 未満であるか、または 59 を超えています。

または

second が 0 未満であるか、または 59 を超えています。

または

millisecond が 0 未満であるか、または 999 を超えています。

- または -

microsecond が 0 未満であるか、または 999 を超えています。

kindDateTimeKind 値のいずれでもありません。

注釈

このコンストラクターは、 yearグレゴリオ暦の年、月、日として、および monthday を解釈します。 別のカレンダーで DateTime 年、月、日を使用して値をインスタンス化するには、 コンストラクターを DateTime(Int32, Int32, Int32, Int32, Int32, Int32, Int32, Int32, Calendar, DateTimeKind) 呼び出します。

日付と時刻のデータの移植性またはタイム ゾーン認識の程度が限られているアプリケーションでは、対応する DateTimeOffset コンストラクターを使用できます。

適用対象

.NET 9 およびその他のバージョン
製品 バージョン
.NET 7, 8, 9

DateTime(Int32, Int32, Int32, Int32, Int32, Int32, Int32, Calendar, DateTimeKind)

ソース:
DateTime.cs
ソース:
DateTime.cs
ソース:
DateTime.cs

DateTime 構造体の新しいインスタンスを、指定した暦の指定した年、月、日、時、分、秒、ミリ秒、および世界協定時刻 (UTC) または現地時刻に初期化します。

public DateTime (int year, int month, int day, int hour, int minute, int second, int millisecond, System.Globalization.Calendar calendar, DateTimeKind kind);

パラメーター

year
Int32

年 (1 から calendar の年数)。

month
Int32

月 (1 から calendar の月数)。

day
Int32

日付 (1 ~ month の日数)。

hour
Int32

時間 (0 から 23)。

minute
Int32

分 (0 から 59)。

second
Int32

秒 (0 から 59)。

millisecond
Int32

ミリ秒 (0 から 999)。

calendar
Calendar

yearmonth、および day を解釈するために使用される暦。

kind
DateTimeKind

yearmonthdayhourminutesecond、および millisecond が、現地時刻、世界協定時刻 (UTC)、またはそのどちらでもない時刻のうち、どれを指定するかを示す列挙値の 1 つ。

例外

calendarnullです。

yearcalendar でサポートされる範囲にありません。

または

month は 1 未満か、calendar の月数を超えています。

または

day が 1 未満であるか、または monthの日数を超えています。

または

hour が 0 未満であるか、または 23 を超えています。

または

minute が 0 未満であるか、または 59 を超えています。

または

second が 0 未満であるか、または 59 を超えています。

または

millisecond が 0 未満であるか、または 999 を超えています。

kindDateTimeKind 値のいずれでもありません。

次の例では、コンストラクターを DateTime(Int32, Int32, Int32, Int32, Int32, Int32, Int32, Calendar, DateTimeKind) 2 回呼び出して 2 つの DateTime 値をインスタンス化します。 最初の呼び出しでは、 オブジェクトを DateTime 使用して値が PersianCalendar インスタンス化されます。 ペルシャ暦はカルチャの既定のカレンダーとして指定できないため、ペルシャ暦に日付を表示するには、その PersianCalendar.GetMonthPersianCalendar.GetDayOfMonth、、および PersianCalendar.GetYear メソッドを個別に呼び出す必要があります。 コンストラクターの 2 番目の呼び出しでは、 オブジェクトを DateTime 使用して値が HijriCalendar インスタンス化されます。 この例では、現在のカルチャをアラビア語 (シリア) に変更し、現在のカルチャの既定のカレンダーをイスラム暦に変更します。 イスラム暦は現在のカルチャの既定のカレンダーであるため、 Console.WriteLine メソッドはそれを使用して日付の書式設定を行います。 以前の現在のカルチャ (この場合は英語 (米国) が復元されると、 Console.WriteLine メソッドは現在のカルチャの既定のグレゴリオ暦を使用して日付の書式を設定します。

using System;
using System.Globalization;
using System.Text.RegularExpressions;
using System.Threading;

public class Example
{
   public static void Main()
   {
      Console.WriteLine("Using the Persian Calendar:");
      PersianCalendar persian = new PersianCalendar();
      DateTime date1 = new DateTime(1389, 5, 27, 16, 32, 18, 500,
                                    persian, DateTimeKind.Local);
      Console.WriteLine("{0:M/dd/yyyy h:mm:ss.fff tt} {1}", date1, date1.Kind);
      Console.WriteLine("{0}/{1}/{2} {3}{8}{4:D2}{8}{5:D2}.{6:G3} {7}\n",
                                       persian.GetMonth(date1),
                                       persian.GetDayOfMonth(date1),
                                       persian.GetYear(date1),
                                       persian.GetHour(date1),
                                       persian.GetMinute(date1),
                                       persian.GetSecond(date1),
                                       persian.GetMilliseconds(date1),
                                       date1.Kind,
                                       DateTimeFormatInfo.CurrentInfo.TimeSeparator);

      Console.WriteLine("Using the Hijri Calendar:");
      // Get current culture so it can later be restored.
      CultureInfo dftCulture = Thread.CurrentThread.CurrentCulture;

      // Define strings for use in composite formatting.
      string dFormat;
      string fmtString;
      // Define Hijri calendar.
      HijriCalendar hijri = new HijriCalendar();
      // Make ar-SY the current culture and Hijri the current calendar.
      Thread.CurrentThread.CurrentCulture = new CultureInfo("ar-SY");
      CultureInfo current = CultureInfo.CurrentCulture;
      current.DateTimeFormat.Calendar = hijri;
      dFormat = current.DateTimeFormat.ShortDatePattern;
      // Ensure year is displayed as four digits.
      dFormat = Regex.Replace(dFormat, "/yy$", "/yyyy") + " H:mm:ss.fff";
      fmtString = "{0} culture using the {1} calendar: {2:" + dFormat + "} {3}";
      DateTime date2 = new DateTime(1431, 9, 9, 16, 32, 18, 500,
                                    hijri, DateTimeKind.Local);
      Console.WriteLine(fmtString, current, GetCalendarName(hijri),
                        date2, date2.Kind);

      // Restore previous culture.
      Thread.CurrentThread.CurrentCulture = dftCulture;
      dFormat = DateTimeFormatInfo.CurrentInfo.ShortDatePattern +" H:mm:ss.fff";
      fmtString = "{0} culture using the {1} calendar: {2:" + dFormat + "} {3}";
      Console.WriteLine(fmtString,
                        CultureInfo.CurrentCulture,
                        GetCalendarName(CultureInfo.CurrentCulture.Calendar),
                        date2, date2.Kind);
   }

   private static string GetCalendarName(Calendar cal)
   {
      return Regex.Match(cal.ToString(), "\\.(\\w+)Calendar").Groups[1].Value;
   }
}
// The example displays the following output:
//    Using the Persian Calendar:
//    8/18/2010 4:32:18.500 PM Local
//    5/27/1389 16:32:18.500 Local
//
//    Using the Hijri Calendar:
//    ar-SY culture using the Hijri calendar: 09/09/1431 16:32:18.500 Local
//    en-US culture using the Gregorian calendar: 8/18/2010 16:32:18.500 Local

注釈

、、および day パラメーターに使用monthyearできる値は、 パラメーターによって異なりますcalendar。 指定した日付と時刻を を使用して calendar表現できない場合、例外がスローされます。

日付と時刻のデータの移植性またはタイム ゾーン認識の程度が限られているアプリケーションでは、対応する DateTimeOffset コンストラクターを使用できます。

重要

和暦の時代 (年号) は天皇の代に基づいているため、変更されることが予想されます。 たとえば、JapaneseCalendarJapaneseLunisolarCalendar において、2019 年 5 月 1 日から令和時代が始まることになりました。 このような時代 (年号) の変更は、これらのカレンダーを使用するすべてのアプリケーションに影響します。 詳細と、アプリケーションが影響を受けるかどうかを判断するには、「 .NET での日本語カレンダーでの新しい時代 (年号の処理)」を参照してください。 Windows システムでアプリケーションをテストして時代 (年号) の変更に対する準備を確認する方法については、「 日本の時代 (年号) の変更に備える」を参照してください。 複数の時代 (年号) を含むカレンダーをサポートする .NET の機能と、複数の時代 (年号) をサポートするカレンダーを使用する場合のベスト プラクティスについては、「 年号の使用」を参照してください。

名前空間にはSystem.Globalization、 や JulianCalendarを含むGregorianCalendarいくつかのカレンダーが用意されています。

適用対象

.NET 9 およびその他のバージョン
製品 バージョン
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1

DateTime(Int32, Int32, Int32, Int32, Int32, Int32, Int32, Int32)

ソース:
DateTime.cs
ソース:
DateTime.cs
ソース:
DateTime.cs

DateTime 構造体の新しいインスタンスを、指定した暦の指定した年、月、日、時、分、秒、ミリ秒、および世界協定時刻 (UTC) または現地時刻に初期化します。

public DateTime (int year, int month, int day, int hour, int minute, int second, int millisecond, int microsecond);

パラメーター

year
Int32

年 (1 から 9999)。

month
Int32

月 (1 ~ 12)。

day
Int32

日付 (1 ~ month の日数)。

hour
Int32

時間 (0 から 23)。

minute
Int32

分 (0 から 59)。

second
Int32

秒 (0 から 59)。

millisecond
Int32

ミリ秒 (0 から 999)。

microsecond
Int32

マイクロ秒 (0 ~ 999)。

例外

year が 1 未満であるか、または 9999 を超えています。

または

month が 1 未満であるか、または 12 を超えています。

または

day が 1 未満であるか、または monthの日数を超えています。

または

hour が 0 未満であるか、または 23 を超えています。

または

minute が 0 未満であるか、または 59 を超えています。

または

second が 0 未満であるか、または 59 を超えています。

または

millisecond が 0 未満であるか、または 999 を超えています。

- または -

microsecond が 0 未満であるか、または 999 を超えています。

注釈

このコンストラクターは、 yearグレゴリオ暦の年、月、日として、 month および day を解釈します。 別のカレンダーで DateTime 年、月、日を使用して値をインスタンス化するには、 コンストラクターを DateTime(Int32, Int32, Int32, Int32, Int32, Int32, Int32, Int32, Calendar) 呼び出します。

Kind プロパティは Unspecified に初期化されます。

日付と時刻のデータの移植性またはタイム ゾーン認識の程度が限られているアプリケーションでは、対応する DateTimeOffset コンストラクターを使用できます。

適用対象

.NET 9 およびその他のバージョン
製品 バージョン
.NET 7, 8, 9

DateTime(Int32, Int32, Int32, Int32, Int32, Int32, Int32, Calendar)

ソース:
DateTime.cs
ソース:
DateTime.cs
ソース:
DateTime.cs

DateTime 構造体の新しいインスタンスを、指定した暦の指定した年、月、日、時、分、秒、およびミリ秒に初期化します。

public DateTime (int year, int month, int day, int hour, int minute, int second, int millisecond, System.Globalization.Calendar calendar);

パラメーター

year
Int32

年 (1 から calendar の年数)。

month
Int32

月 (1 から calendar の月数)。

day
Int32

日付 (1 ~ month の日数)。

hour
Int32

時間 (0 から 23)。

minute
Int32

分 (0 から 59)。

second
Int32

秒 (0 から 59)。

millisecond
Int32

ミリ秒 (0 から 999)。

calendar
Calendar

yearmonth、および day を解釈するために使用される暦。

例外

calendarnullです。

yearcalendar でサポートされる範囲にありません。

または

month は 1 未満か、calendar の月数を超えています。

または

day が 1 未満であるか、または monthの日数を超えています。

または

hour が 0 未満であるか、または 23 を超えています。

または

minute が 0 未満であるか、または 59 を超えています。

または

second が 0 未満であるか、または 59 を超えています。

または

millisecond が 0 未満であるか、または 999 を超えています。

次の例では、コンストラクターを DateTime(Int32, Int32, Int32, Int32, Int32, Int32, Int32, Calendar) 2 回呼び出して 2 つの DateTime 値をインスタンス化します。 最初の呼び出しでは、 オブジェクトを DateTime 使用して値が PersianCalendar インスタンス化されます。 ペルシャ暦はカルチャの既定のカレンダーとして指定できないため、ペルシャ暦に日付を表示するには、その PersianCalendar.GetMonthPersianCalendar.GetDayOfMonth、、および PersianCalendar.GetYear メソッドを個別に呼び出す必要があります。 コンストラクターの 2 番目の呼び出しでは、 オブジェクトを DateTime 使用して値が HijriCalendar インスタンス化されます。 この例では、現在のカルチャをアラビア語 (シリア) に変更し、現在のカルチャの既定のカレンダーをイスラム暦に変更します。 イスラム暦は現在のカルチャの既定のカレンダーであるため、 Console.WriteLine メソッドはそれを使用して日付の書式設定を行います。 以前の現在のカルチャ (この場合は英語 (米国) が復元されると、 Console.WriteLine メソッドは現在のカルチャの既定のグレゴリオ暦を使用して日付の書式を設定します。

using System;
using System.Globalization;
using System.Text.RegularExpressions;
using System.Threading;

public class Example
{
   public static void Main()
   {
      Console.WriteLine("Using the Persian Calendar:");
      PersianCalendar persian = new PersianCalendar();
      DateTime date1 = new DateTime(1389, 5, 27, 16, 32, 18, 500, persian);
      Console.WriteLine(date1.ToString("M/dd/yyyy h:mm:ss.fff tt"));
      Console.WriteLine("{0}/{1}/{2} {3}{7}{4:D2}{7}{5:D2}.{6:G3}\n",
                                       persian.GetMonth(date1),
                                       persian.GetDayOfMonth(date1),
                                       persian.GetYear(date1),
                                       persian.GetHour(date1),
                                       persian.GetMinute(date1),
                                       persian.GetSecond(date1),
                                       persian.GetMilliseconds(date1),
                                       DateTimeFormatInfo.CurrentInfo.TimeSeparator);

      Console.WriteLine("Using the Hijri Calendar:");
      // Get current culture so it can later be restored.
      CultureInfo dftCulture = Thread.CurrentThread.CurrentCulture;

      // Define strings for use in composite formatting.
      string dFormat;
      string fmtString;
      // Define Hijri calendar.
      HijriCalendar hijri = new HijriCalendar();
      // Make ar-SY the current culture and Hijri the current calendar.
      Thread.CurrentThread.CurrentCulture = new CultureInfo("ar-SY");
      CultureInfo current = CultureInfo.CurrentCulture;
      current.DateTimeFormat.Calendar = hijri;
      dFormat = current.DateTimeFormat.ShortDatePattern;
      // Ensure year is displayed as four digits.
      dFormat = Regex.Replace(dFormat, "/yy$", "/yyyy") + " H:mm:ss.fff";
      fmtString = "{0} culture using the {1} calendar: {2:" + dFormat + "}";
      DateTime date2 = new DateTime(1431, 9, 9, 16, 32, 18, 500, hijri);
      Console.WriteLine(fmtString, current, GetCalendarName(hijri), date2);

      // Restore previous culture.
      Thread.CurrentThread.CurrentCulture = dftCulture;
      dFormat = DateTimeFormatInfo.CurrentInfo.ShortDatePattern +" H:mm:ss.fff";
      fmtString = "{0} culture using the {1} calendar: {2:" + dFormat + "}";
      Console.WriteLine(fmtString,
                        CultureInfo.CurrentCulture,
                        GetCalendarName(CultureInfo.CurrentCulture.Calendar),
                        date2);
   }

   private static string GetCalendarName(Calendar cal)
   {
      return Regex.Match(cal.ToString(), "\\.(\\w+)Calendar").Groups[1].Value;
   }
}
// The example displays the following output:
//       8/18/2010 4:32:18.500 PM
//       5/27/1389 16:32:18.500
//
//       Using the Hijri Calendar:
//       ar-SY culture using the Hijri calendar: 09/09/1431 16:32:18.500
//       en-US culture using the Gregorian calendar: 8/18/2010 16:32:18.500

注釈

Kind プロパティは Unspecified に初期化されます。

、、および day に使用yearできる値は、 によって異なりますcalendarmonth 指定した日付と時刻を を使用して calendar表現できない場合、例外がスローされます。

日付と時刻のデータの移植性またはタイム ゾーン認識の程度が限られているアプリケーションでは、対応する DateTimeOffset コンストラクターを使用できます。

重要

和暦の時代 (年号) は天皇の代に基づいているため、変更されることが予想されます。 たとえば、JapaneseCalendarJapaneseLunisolarCalendar において、2019 年 5 月 1 日から令和時代が始まることになりました。 このような時代 (年号) の変更は、これらのカレンダーを使用するすべてのアプリケーションに影響します。 詳細と、アプリケーションが影響を受けるかどうかを判断するには、「 .NET での日本語カレンダーでの新しい時代 (年号の処理)」を参照してください。 Windows システムでアプリケーションをテストして時代 (年号) の変更に対する準備を確認する方法については、「 日本の時代 (年号) の変更に備える」を参照してください。 複数の時代 (年号) を含むカレンダーをサポートする .NET の機能と、複数の時代 (年号) をサポートするカレンダーを使用する場合のベスト プラクティスについては、「 年号の使用」を参照してください。

名前空間にはSystem.Globalization、 や JulianCalendarを含むGregorianCalendarいくつかのカレンダーが用意されています。

こちらもご覧ください

適用対象

.NET 9 およびその他のバージョン
製品 バージョン
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1

DateTime(Int32, Int32, Int32, Int32, Int32, Int32, Int32, DateTimeKind)

ソース:
DateTime.cs
ソース:
DateTime.cs
ソース:
DateTime.cs

DateTime 構造体の新しいインスタンスを、指定した年、月、日、時、分、秒、ミリ秒、および世界協定時刻 (UTC) または現地時刻に初期化します。

public DateTime (int year, int month, int day, int hour, int minute, int second, int millisecond, DateTimeKind kind);

パラメーター

year
Int32

年 (1 から 9999)。

month
Int32

月 (1 ~ 12)。

day
Int32

日付 (1 ~ month の日数)。

hour
Int32

時間 (0 から 23)。

minute
Int32

分 (0 から 59)。

second
Int32

秒 (0 から 59)。

millisecond
Int32

ミリ秒 (0 から 999)。

kind
DateTimeKind

yearmonthdayhourminutesecond、および millisecond が、現地時刻、世界協定時刻 (UTC)、またはそのどちらでもない時刻のうち、どれを指定するかを示す列挙値の 1 つ。

例外

year が 1 未満であるか、または 9999 を超えています。

または

month が 1 未満であるか、または 12 を超えています。

または

day が 1 未満であるか、または monthの日数を超えています。

または

hour が 0 未満であるか、または 23 を超えています。

または

minute が 0 未満であるか、または 59 を超えています。

または

second が 0 未満であるか、または 59 を超えています。

または

millisecond が 0 未満であるか、または 999 を超えています。

kindDateTimeKind 値のいずれでもありません。

次の例では、 コンストラクターを DateTime(Int32, Int32, Int32, Int32, Int32, Int32, Int32, DateTimeKind) 使用して値を DateTime インスタンス化します。

DateTime date1 = new DateTime(2010, 8, 18, 16, 32, 18, 500,
                              DateTimeKind.Local);
Console.WriteLine("{0:M/dd/yyyy h:mm:ss.fff tt} {1}", date1, date1.Kind);
// The example displays the following output, in this case for en-us culture:
//      8/18/2010 4:32:18.500 PM Local

注釈

このコンストラクターは、monthグレゴリオ暦の年、月、日として、、dayおよび を解釈yearします。 別のカレンダーで DateTime 年、月、日を使用して値をインスタンス化するには、 コンストラクターを DateTime(Int32, Int32, Int32, Int32, Int32, Int32, Int32, Calendar, DateTimeKind) 呼び出します。

日付と時刻のデータの移植性またはタイム ゾーン認識の程度が限られているアプリケーションでは、対応する DateTimeOffset コンストラクターを使用できます。

適用対象

.NET 9 およびその他のバージョン
製品 バージョン
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1
UWP 10.0

DateTime(Int32, Int32, Int32, Int32, Int32, Int32, Int32)

ソース:
DateTime.cs
ソース:
DateTime.cs
ソース:
DateTime.cs

DateTime 構造体の新しいインスタンスを、指定した年、月、日、時、分、秒、およびミリ秒に初期化します。

public DateTime (int year, int month, int day, int hour, int minute, int second, int millisecond);

パラメーター

year
Int32

年 (1 から 9999)。

month
Int32

月 (1 ~ 12)。

day
Int32

日付 (1 ~ month の日数)。

hour
Int32

時間 (0 から 23)。

minute
Int32

分 (0 から 59)。

second
Int32

秒 (0 から 59)。

millisecond
Int32

ミリ秒 (0 から 999)。

例外

year が 1 未満であるか、または 9999 を超えています。

または

month が 1 未満であるか、または 12 を超えています。

または

day が 1 未満であるか、または monthの日数を超えています。

または

hour が 0 未満であるか、または 23 を超えています。

または

minute が 0 未満であるか、または 59 を超えています。

または

second が 0 未満であるか、または 59 を超えています。

または

millisecond が 0 未満であるか、または 999 を超えています。

次の例では、 コンストラクターを DateTime(Int32, Int32, Int32, Int32, Int32, Int32, Int32) 使用して値を DateTime インスタンス化します。

DateTime date1 = new DateTime(2010, 8, 18, 16, 32, 18, 500);
Console.WriteLine(date1.ToString("M/dd/yyyy h:mm:ss.fff tt"));
// The example displays the following output, in this case for en-us culture:
//      8/18/2010 4:32:18.500 PM

注釈

このコンストラクターは、monthグレゴリオ暦の年、月、日として、、dayおよび を解釈yearします。 別のカレンダーで DateTime 年、月、日を使用して値をインスタンス化するには、 コンストラクターを DateTime(Int32, Int32, Int32, Int32, Int32, Int32, Int32, Calendar) 呼び出します。

Kind プロパティは Unspecified に初期化されます。

日付と時刻のデータの移植性またはタイム ゾーン認識の程度が限られているアプリケーションでは、対応する DateTimeOffset コンストラクターを使用できます。

こちらもご覧ください

適用対象

.NET 9 およびその他のバージョン
製品 バージョン
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1
UWP 10.0

DateTime(Int32, Int32, Int32, Int32, Int32, Int32, Int32, Int32, Calendar, DateTimeKind)

ソース:
DateTime.cs
ソース:
DateTime.cs
ソース:
DateTime.cs

DateTime 構造体の新しいインスタンスを、指定した暦の指定した年、月、日、時、分、秒、ミリ秒、および世界協定時刻 (UTC) または現地時刻に初期化します。

public DateTime (int year, int month, int day, int hour, int minute, int second, int millisecond, int microsecond, System.Globalization.Calendar calendar, DateTimeKind kind);

パラメーター

year
Int32

年 (1 から calendar の年数)。

month
Int32

月 (1 から calendar の月数)。

day
Int32

日付 (1 ~ month の日数)。

hour
Int32

時間 (0 から 23)。

minute
Int32

分 (0 から 59)。

second
Int32

秒 (0 から 59)。

millisecond
Int32

ミリ秒 (0 から 999)。

microsecond
Int32

マイクロ秒 (0 ~ 999)。

calendar
Calendar

yearmonth、および day を解釈するために使用される暦。

kind
DateTimeKind

yearmonthdayhourminutesecond、および millisecond が、現地時刻、世界協定時刻 (UTC)、またはそのどちらでもない時刻のうち、どれを指定するかを示す列挙値の 1 つ。

例外

calendarnull です

yearcalendar でサポートされる範囲にありません。

または

month は 1 未満か、calendar の月数を超えています。

または

day が 1 未満であるか、または monthの日数を超えています。

または

hour が 0 未満であるか、または 23 を超えています。

または

minute が 0 未満であるか、または 59 を超えています。

または

second が 0 未満であるか、または 59 を超えています。

または

millisecond が 0 未満であるか、または 999 を超えています。

- または -

microsecond が 0 未満であるか、または 999 を超えています。

kindDateTimeKind 値のいずれでもありません。

注釈

、、および day パラメーターに使用monthyearできる値は、 パラメーターによって異なりますcalendar。 指定した日付と時刻を を使用して calendar表現できない場合、例外がスローされます。

日付と時刻のデータの移植性またはタイム ゾーン認識の程度が限られているアプリケーションでは、対応する DateTimeOffset コンストラクターを使用できます。

適用対象

.NET 9 およびその他のバージョン
製品 バージョン
.NET 7, 8, 9

DateTime(Int32, Int32, Int32, Int32, Int32, Int32, DateTimeKind)

ソース:
DateTime.cs
ソース:
DateTime.cs
ソース:
DateTime.cs

DateTime 構造体の新しいインスタンスを、指定した年、月、日、時、分、秒、および世界協定時刻 (UTC) または現地時刻に初期化します。

public DateTime (int year, int month, int day, int hour, int minute, int second, DateTimeKind kind);

パラメーター

year
Int32

年 (1 から 9999)。

month
Int32

月 (1 ~ 12)。

day
Int32

日付 (1 ~ month の日数)。

hour
Int32

時間 (0 から 23)。

minute
Int32

分 (0 から 59)。

second
Int32

秒 (0 から 59)。

kind
DateTimeKind

yearmonthdayhourminute、および second が、現地時刻、世界協定時刻 (UTC)、またはそのどちらでもない時刻のうち、どれを指定するかを示す列挙値の 1 つ。

例外

year が 1 未満であるか、または 9999 を超えています。

または

month が 1 未満であるか、または 12 を超えています。

または

day が 1 未満であるか、または monthの日数を超えています。

または

hour が 0 未満であるか、または 23 を超えています。

または

minute が 0 未満であるか、または 59 を超えています。

または

second が 0 未満であるか、または 59 を超えています。

kindDateTimeKind 値のいずれでもありません。

次の例では、 コンストラクターを DateTime(Int32, Int32, Int32, Int32, Int32, Int32, DateTimeKind) 使用して値を DateTime インスタンス化します。

DateTime date1 = new DateTime(2010, 8, 18, 16, 32, 0, DateTimeKind.Local);
Console.WriteLine("{0} {1}", date1, date1.Kind);
// The example displays the following output, in this case for en-us culture:
//      8/18/2010 4:32:00 PM Local

注釈

このコンストラクターは、monthグレゴリオ暦の年、月、日として、、dayおよび を解釈yearします。 別のカレンダーで DateTime 年、月、日を使用して値をインスタンス化するには、 コンストラクターを DateTime(Int32, Int32, Int32, Int32, Int32, Int32, Int32, Calendar, DateTimeKind) 呼び出します。

日付と時刻のデータの移植性またはタイム ゾーン認識の程度が限られているアプリケーションでは、対応する DateTimeOffset コンストラクターを使用できます。

適用対象

.NET 9 およびその他のバージョン
製品 バージョン
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1
UWP 10.0

DateTime(Int32, Int32, Int32, Int32, Int32, Int32)

ソース:
DateTime.cs
ソース:
DateTime.cs
ソース:
DateTime.cs

DateTime 構造体の新しいインスタンスを、指定した年、月、日、時、分、および秒に初期化します。

public DateTime (int year, int month, int day, int hour, int minute, int second);

パラメーター

year
Int32

年 (1 から 9999)。

month
Int32

月 (1 ~ 12)。

day
Int32

日付 (1 ~ month の日数)。

hour
Int32

時間 (0 から 23)。

minute
Int32

分 (0 から 59)。

second
Int32

秒 (0 から 59)。

例外

year が 1 未満であるか、または 9999 を超えています。

または

month が 1 未満であるか、または 12 を超えています。

または

day が 1 未満であるか、または monthの日数を超えています。

または

hour が 0 未満であるか、または 23 を超えています。

または

minute が 0 未満であるか、または 59 を超えています。

または

second が 0 未満であるか、または 59 を超えています。

次の例では、 コンストラクターを DateTime 使用して値を DateTime インスタンス化します。

DateTime date1 = new DateTime(2010, 8, 18, 16, 32, 0);
Console.WriteLine(date1.ToString());
// The example displays the following output, in this case for en-us culture:
//      8/18/2010 4:32:00 PM

注釈

Kind プロパティは Unspecified に初期化されます。

このコンストラクターは、monthグレゴリオ暦の年、月、日として、、dayおよび を解釈yearします。 別のカレンダーで DateTime 年、月、日を使用して値をインスタンス化するには、 コンストラクターを DateTime(Int32, Int32, Int32, Int32, Int32, Int32, Calendar) 呼び出します。

日付と時刻のデータの移植性またはタイム ゾーン認識の程度が限られているアプリケーションでは、対応する DateTimeOffset コンストラクターを使用できます。

適用対象

.NET 9 およびその他のバージョン
製品 バージョン
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1
UWP 10.0

DateTime(Int32, Int32, Int32, Calendar)

ソース:
DateTime.cs
ソース:
DateTime.cs
ソース:
DateTime.cs

DateTime 構造体の新しいインスタンスを、指定した暦の指定した年、月、および日に初期化します。

public DateTime (int year, int month, int day, System.Globalization.Calendar calendar);

パラメーター

year
Int32

年 (1 から calendar の年数)。

month
Int32

月 (1 から calendar の月数)。

day
Int32

日付 (1 ~ month の日数)。

calendar
Calendar

yearmonth、および day を解釈するために使用される暦。

例外

calendarnullです。

yearcalendar でサポートされる範囲にありません。

または

month は 1 未満か、calendar の月数を超えています。

または

day が 1 未満であるか、または monthの日数を超えています。

次の例では、コンストラクターを DateTime(Int32, Int32, Int32, Calendar) 2 回呼び出して 2 つの DateTime 値をインスタンス化します。 最初の呼び出しでは、 オブジェクトを DateTime 使用して値が PersianCalendar インスタンス化されます。 ペルシャ暦はカルチャの既定のカレンダーとして指定できないため、ペルシャ暦に日付を表示するには、その PersianCalendar.GetMonthPersianCalendar.GetDayOfMonth、、および PersianCalendar.GetYear メソッドを個別に呼び出す必要があります。 コンストラクターの 2 番目の呼び出しでは、 オブジェクトを DateTime 使用して値が HijriCalendar インスタンス化されます。 この例では、現在のカルチャをアラビア語 (シリア) に変更し、現在のカルチャの既定のカレンダーをイスラム暦に変更します。 イスラム暦は現在のカルチャの既定のカレンダーであるため、 Console.WriteLine メソッドはそれを使用して日付の書式設定を行います。 以前の現在のカルチャ (この場合は英語 (米国) が復元されると、 Console.WriteLine メソッドは現在のカルチャの既定のグレゴリオ暦を使用して日付の書式を設定します。

using System;
using System.Globalization;
using System.Text.RegularExpressions;
using System.Threading;

public class Example
{
   public static void Main()
   {
      Console.WriteLine("Using the Persian Calendar:");
      PersianCalendar persian = new PersianCalendar();
      DateTime date1 = new DateTime(1389, 5, 27, persian);
      Console.WriteLine(date1.ToString());
      Console.WriteLine("{0}/{1}/{2}\n", persian.GetMonth(date1),
                                       persian.GetDayOfMonth(date1),
                                       persian.GetYear(date1));

      Console.WriteLine("Using the Hijri Calendar:");
      // Get current culture so it can later be restored.
      CultureInfo dftCulture = Thread.CurrentThread.CurrentCulture;

      // Define Hijri calendar.
      HijriCalendar hijri = new HijriCalendar();
      // Make ar-SY the current culture and Hijri the current calendar.
      Thread.CurrentThread.CurrentCulture = new CultureInfo("ar-SY");
      CultureInfo current = CultureInfo.CurrentCulture;
      current.DateTimeFormat.Calendar = hijri;
      string dFormat = current.DateTimeFormat.ShortDatePattern;
      // Ensure year is displayed as four digits.
      dFormat = Regex.Replace(dFormat, "/yy$", "/yyyy");
      current.DateTimeFormat.ShortDatePattern = dFormat;
      DateTime date2 = new DateTime(1431, 9, 9, hijri);
      Console.WriteLine("{0} culture using the {1} calendar: {2:d}", current,
                        GetCalendarName(hijri), date2);

      // Restore previous culture.
      Thread.CurrentThread.CurrentCulture = dftCulture;
      Console.WriteLine("{0} culture using the {1} calendar: {2:d}",
                        CultureInfo.CurrentCulture,
                        GetCalendarName(CultureInfo.CurrentCulture.Calendar),
                        date2);
   }

   private static string GetCalendarName(Calendar cal)
   {
      return Regex.Match(cal.ToString(), "\\.(\\w+)Calendar").Groups[1].Value;
   }
}
// The example displays the following output:
//       Using the Persian Calendar:
//       8/18/2010 12:00:00 AM
//       5/27/1389
//
//       Using the Hijri Calendar:
//       ar-SY culture using the Hijri calendar: 09/09/1431
//       en-US culture using the Gregorian calendar: 8/18/2010

注釈

結果 DateTime の時刻は午前 0 時 (00:00:00) です。 Kind プロパティは Unspecified に初期化されます。

、、および day に使用yearできる値は、 によって異なりますcalendarmonth 指定した日付と時刻を を使用して calendar表現できない場合、例外がスローされます。

重要

和暦の時代 (年号) は天皇の代に基づいているため、変更されることが予想されます。 たとえば、JapaneseCalendarJapaneseLunisolarCalendar において、2019 年 5 月 1 日から令和時代が始まることになりました。 このような時代 (年号) の変更は、これらのカレンダーを使用するすべてのアプリケーションに影響します。 詳細と、アプリケーションが影響を受けるかどうかを判断するには、「 .NET での日本語カレンダーでの新しい時代 (年号の処理)」を参照してください。 Windows システムでアプリケーションをテストして時代 (年号) の変更に対する準備を確認する方法については、「 日本の時代 (年号) の変更に備える」を参照してください。 複数の時代 (年号) を含むカレンダーをサポートする .NET の機能と、複数の時代 (年号) をサポートするカレンダーを使用する場合のベスト プラクティスについては、「 年号の使用」を参照してください。

名前空間にはSystem.Globalization、 や JulianCalendarを含むGregorianCalendarいくつかのカレンダーが用意されています。

こちらもご覧ください

適用対象

.NET 9 およびその他のバージョン
製品 バージョン
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1

DateTime(Int32, Int32, Int32)

ソース:
DateTime.cs
ソース:
DateTime.cs
ソース:
DateTime.cs

DateTime 構造体の新しいインスタンスを、指定した年、月、日に初期化します。

public DateTime (int year, int month, int day);

パラメーター

year
Int32

年 (1 から 9999)。

month
Int32

月 (1 ~ 12)。

day
Int32

日付 (1 ~ month の日数)。

例外

year が 1 未満であるか、または 9999 を超えています。

または

month が 1 未満であるか、または 12 を超えています。

または

day が 1 未満であるか、または monthの日数を超えています。

次の例では、 コンストラクターを DateTime(Int32, Int32, Int32) 使用して値を DateTime インスタンス化します。 この例では、このオーバーロードによって、時刻コンポーネントが午前 0 時 (または 0:00) の値を作成 DateTime することも示されています。

DateTime date1 = new DateTime(2010, 8, 18);
Console.WriteLine(date1.ToString());
// The example displays the following output:
//      8/18/2010 12:00:00 AM

注釈

このコンストラクターは、yearmonthdayグレゴリオ暦の年、月、日として、、および を解釈します。 別のカレンダーで DateTime 年、月、日を使用して値をインスタンス化するには、 コンストラクターを DateTime(Int32, Int32, Int32, Calendar) 呼び出します。

結果 DateTime の時刻は午前 0 時 (00:00:00) です。 Kind プロパティは DateTimeKind.Unspecified に初期化されます。

適用対象

.NET 9 およびその他のバージョン
製品 バージョン
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1
UWP 10.0

DateTime(DateOnly, TimeOnly, DateTimeKind)

ソース:
DateTime.cs
ソース:
DateTime.cs

構造体の新しいインスタンスをDateTime、指定した に初期化し、TimeOnly指定した DateOnly を尊重しますDateTimeKind

public DateTime (DateOnly date, TimeOnly time, DateTimeKind kind);

パラメーター

date
DateOnly

日付部分。

time
TimeOnly

時間部分。

kind
DateTimeKind

現地時刻、協定世界時 (UTC)、またはどちらも指定しないかどうかをdatetime示す列挙値の 1 つ。

適用対象

.NET 9 および .NET 8
製品 バージョン
.NET 8, 9

DateTime(Int64, DateTimeKind)

ソース:
DateTime.cs
ソース:
DateTime.cs
ソース:
DateTime.cs

DateTime 構造体の新しいインスタンスを、指定したタイマー刻み数と世界協定時刻 (UTC) または現地時刻に初期化します。

public DateTime (long ticks, DateTimeKind kind);

パラメーター

ticks
Int64

日時をグレゴリオ暦の 0001 年 1 月 1 日の 00:00:00.000 以降の経過時間 (100 ナノ秒単位) で表した値。

kind
DateTimeKind

ticks が、現地時刻、世界協定時刻 (UTC)、またはそのどちらでもない時刻のうち、どれを指定するかを示す列挙値の 1 つ。

例外

ticksDateTime.MinValue より小さいか、 DateTime.MaxValue より大きい。

kindDateTimeKind 値のいずれでもありません。

注釈

日付と時刻のデータの移植性またはタイム ゾーン認識の程度が限られているアプリケーションでは、対応する DateTimeOffset コンストラクターを使用できます。

適用対象

.NET 9 およびその他のバージョン
製品 バージョン
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1
UWP 10.0

DateTime(DateOnly, TimeOnly)

ソース:
DateTime.cs
ソース:
DateTime.cs

構造体の新しいインスタンスをDateTime、指定した と TimeOnlyに初期化しますDateOnly。 新しいインスタンスの種類は になります Unspecified

public DateTime (DateOnly date, TimeOnly time);

パラメーター

date
DateOnly

日付部分。

time
TimeOnly

時間部分。

適用対象

.NET 9 および .NET 8
製品 バージョン
.NET 8, 9

DateTime(Int32, Int32, Int32, Int32, Int32, Int32, Calendar)

ソース:
DateTime.cs
ソース:
DateTime.cs
ソース:
DateTime.cs

DateTime 構造体の新しいインスタンスを、指定した暦の指定した年、月、日、時、分、および秒に初期化します。

public DateTime (int year, int month, int day, int hour, int minute, int second, System.Globalization.Calendar calendar);

パラメーター

year
Int32

年 (1 から calendar の年数)。

month
Int32

月 (1 から calendar の月数)。

day
Int32

日付 (1 ~ month の日数)。

hour
Int32

時間 (0 から 23)。

minute
Int32

分 (0 から 59)。

second
Int32

秒 (0 から 59)。

calendar
Calendar

yearmonth、および day を解釈するために使用される暦。

例外

calendarnullです。

yearcalendar でサポートされる範囲にありません。

または

month は 1 未満か、calendar の月数を超えています。

または

day が 1 未満であるか、または monthの日数を超えています。

または

hour が 0 未満であるか、または 23 を超えています。

または

minute が 0 未満であるか、または 59 を超えています。

または

second が 0 未満であるか、または 59 を超えています。

次の例では、コンストラクターを DateTime(Int32, Int32, Int32, Int32, Int32, Int32, Calendar) 2 回呼び出して 2 つの DateTime 値をインスタンス化します。 最初の呼び出しでは、 オブジェクトを DateTime 使用して値が PersianCalendar インスタンス化されます。 ペルシャ暦はカルチャの既定のカレンダーとして指定できないため、ペルシャ暦に日付を表示するには、その PersianCalendar.GetMonthPersianCalendar.GetDayOfMonth、、および PersianCalendar.GetYear メソッドを個別に呼び出す必要があります。 コンストラクターの 2 番目の呼び出しでは、 オブジェクトを DateTime 使用して値が HijriCalendar インスタンス化されます。 この例では、現在のカルチャをアラビア語 (シリア) に変更し、現在のカルチャの既定のカレンダーをイスラム暦に変更します。 イスラム暦は現在のカルチャの既定のカレンダーであるため、 Console.WriteLine メソッドはそれを使用して日付の書式設定を行います。 以前の現在のカルチャ (この場合は英語 (米国) が復元されると、 Console.WriteLine メソッドは現在のカルチャの既定のグレゴリオ暦を使用して日付の書式を設定します。

using System;
using System.Globalization;
using System.Text.RegularExpressions;
using System.Threading;

public class Example
{
   public static void Main()
   {
      Console.WriteLine("Using the Persian Calendar:");
      PersianCalendar persian = new PersianCalendar();
      DateTime date1 = new DateTime(1389, 5, 27, 16, 32, 0, persian);
      Console.WriteLine(date1.ToString());
      Console.WriteLine("{0}/{1}/{2} {3}{6}{4:D2}{6}{5:D2}\n",
                                       persian.GetMonth(date1),
                                       persian.GetDayOfMonth(date1),
                                       persian.GetYear(date1),
                                       persian.GetHour(date1),
                                       persian.GetMinute(date1),
                                       persian.GetSecond(date1),
                                       DateTimeFormatInfo.CurrentInfo.TimeSeparator);

      Console.WriteLine("Using the Hijri Calendar:");
      // Get current culture so it can later be restored.
      CultureInfo dftCulture = Thread.CurrentThread.CurrentCulture;

      // Define Hijri calendar.
      HijriCalendar hijri = new HijriCalendar();
      // Make ar-SY the current culture and Hijri the current calendar.
      Thread.CurrentThread.CurrentCulture = new CultureInfo("ar-SY");
      CultureInfo current = CultureInfo.CurrentCulture;
      current.DateTimeFormat.Calendar = hijri;
      string dFormat = current.DateTimeFormat.ShortDatePattern;
      // Ensure year is displayed as four digits.
      dFormat = Regex.Replace(dFormat, "/yy$", "/yyyy");
      current.DateTimeFormat.ShortDatePattern = dFormat;
      DateTime date2 = new DateTime(1431, 9, 9, 16, 32, 18, hijri);
      Console.WriteLine("{0} culture using the {1} calendar: {2:g}", current,
                        GetCalendarName(hijri), date2);

      // Restore previous culture.
      Thread.CurrentThread.CurrentCulture = dftCulture;
      Console.WriteLine("{0} culture using the {1} calendar: {2:g}",
                        CultureInfo.CurrentCulture,
                        GetCalendarName(CultureInfo.CurrentCulture.Calendar),
                        date2);
   }

   private static string GetCalendarName(Calendar cal)
   {
      return Regex.Match(cal.ToString(), "\\.(\\w+)Calendar").Groups[1].Value;
   }
}
// The example displays the following output:
//       Using the Persian Calendar:
//       8/18/2010 4:32:00 PM
//       5/27/1389 16:32:00
//
//       Using the Hijri Calendar:
//       ar-SY culture using the Hijri calendar: 09/09/1431 04:32 م
//       en-US culture using the Gregorian calendar: 8/18/2010 4:32 PM

注釈

Kind プロパティは Unspecified に初期化されます。

、、および day に使用yearできる値は、 によって異なりますcalendarmonth 指定した日付と時刻を を使用して calendar表現できない場合、例外がスローされます。

重要

和暦の時代 (年号) は天皇の代に基づいているため、変更されることが予想されます。 たとえば、JapaneseCalendarJapaneseLunisolarCalendar において、2019 年 5 月 1 日から令和時代が始まることになりました。 このような時代 (年号) の変更は、これらのカレンダーを使用するすべてのアプリケーションに影響します。 詳細と、アプリケーションが影響を受けるかどうかを判断するには、「 .NET での日本語カレンダーでの新しい時代 (年号の処理)」を参照してください。 Windows システムでアプリケーションをテストして時代 (年号) の変更に対する準備を確認する方法については、「 日本の時代 (年号) の変更に備える」を参照してください。 複数の時代 (年号) を含むカレンダーをサポートする .NET の機能と、複数の時代 (年号) をサポートするカレンダーを使用する場合のベスト プラクティスについては、「 年号の使用」を参照してください。

名前空間にはSystem.Globalization、 や JulianCalendarを含むGregorianCalendarいくつかのカレンダーが用意されています。

こちらもご覧ください

適用対象

.NET 9 およびその他のバージョン
製品 バージョン
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1