DateTime コンストラクター
定義
重要
一部の情報は、リリース前に大きく変更される可能性があるプレリリースされた製品に関するものです。 Microsoft は、ここに記載されている情報について、明示または黙示を問わず、一切保証しません。
DateTime構造体の新しいインスタンスを初期化します。
オーバーロード
DateTime(Int64)
- ソース:
- DateTime.cs
- ソース:
- DateTime.cs
- ソース:
- DateTime.cs
- ソース:
- DateTime.cs
- ソース:
- DateTime.cs
指定したティック数に DateTime 構造体の新しいインスタンスを初期化します。
public:
DateTime(long ticks);
public DateTime(long ticks);
new DateTime : int64 -> DateTime
Public Sub New (ticks As Long)
パラメーター
- ticks
- Int64
グレゴリオ暦の 0001 年 1 月 1 日 00:00:00.000 から経過した 100 ナノ秒間隔の数で表される日付と時刻。
例外
ticks が DateTime.MinValue より小さいか、 DateTime.MaxValue より大きい値です。
例
次の例は、 DateTime コンストラクターの 1 つを示しています。
// This example demonstrates the DateTime(Int64) constructor.
open System
open System.Globalization
// Create a DateTime for the maximum date and time using ticks.
let dt1 = DateTime DateTime.MaxValue.Ticks
// Create a DateTime for the minimum date and time using ticks.
let dt2 = 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.
let ticks = DateTime(1979, 07, 28, 22, 35, 5, CultureInfo("en-US", false).Calendar).Ticks
let dt3 = DateTime ticks
printfn $"""1) The maximum date and time is {dt1.ToString "MM-dd/yyyy hh:mm:ss tt"}"""
printfn $"""2) The minimum date and time is {dt2.ToString "MM/dd/yyyy hh:mm:ss tt"}"""
printfn $"""3) The custom date and time is {dt3.ToString "MM/dd/yyyy hh:mm:ss tt"}"""
printfn $"\nThe custom date and time is created from {ticks:N0} 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.
// 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.
*/
' This example demonstrates the DateTime(Int64) constructor.
Imports System.Globalization
Class Sample
Public Shared Sub 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.
Dim format As String = "{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.
Dim dt1 As New DateTime(DateTime.MaxValue.Ticks)
' Create a DateTime for the minimum date and time using ticks.
Dim dt2 As 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.
Dim ticks As Long = New DateTime(1979, 7, 28, 22, 35, 5, _
New CultureInfo("en-US", False).Calendar).Ticks
Dim dt3 As New DateTime(ticks)
Console.WriteLine(format, 1, "maximum", dt1)
Console.WriteLine(format, 2, "minimum", dt2)
Console.WriteLine(format, 3, "custom ", dt3)
Console.WriteLine(vbCrLf & "The custom date and time is created from {0:N0} ticks.", ticks)
End Sub
End Class
'
'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 コンストラクターを使用できます。
適用対象
DateTime(Int32, Int32, Int32, Int32, Int32, Int32, Int32, Int32, Calendar)
- ソース:
- DateTime.cs
- ソース:
- DateTime.cs
- ソース:
- 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);
public DateTime(int year, int month, int day, int hour, int minute, int second, int millisecond, int microsecond, System.Globalization.Calendar calendar);
new DateTime : int * int * int * int * int * int * int * int * System.Globalization.Calendar -> DateTime
Public Sub New (year As Integer, month As Integer, day As Integer, hour As Integer, minute As Integer, second As Integer, millisecond As Integer, microsecond As Integer, calendar As 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
year、month、およびdayを解釈するために使用されるカレンダー。
例外
calendar は null です
year が、 calendarでサポートされている範囲内にありません。
-又は-
month が 1 未満か、 calendarの月数を超えている。
-又は-
day が 1 未満か、 monthの日数を超えています。
-又は-
hour が 0 より小さいか、23 より大きい値です。
-又は-
minute が 0 未満または 59 より大きい。
-又は-
second が 0 未満または 59 より大きい。
-又は-
millisecond が 0 より小さいか、999 より大きい値です。
-又は-
microsecond が 0 より小さいか、999 より大きい値です。
注釈
year、month、およびdayパラメーターの許容値は、calendar パラメーターによって異なります。
calendarを使用して指定した日時を表すことができない場合は、例外がスローされます。
日付と時刻のデータの移植性またはタイム ゾーン認識の程度が限られているアプリケーションでは、対応する DateTimeOffset コンストラクターを使用できます。
適用対象
DateTime(Int32, Int32, Int32, Int32, Int32, Int32, Int32, Int32, DateTimeKind)
- ソース:
- DateTime.cs
- ソース:
- DateTime.cs
- ソース:
- 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);
public DateTime(int year, int month, int day, int hour, int minute, int second, int millisecond, int microsecond, DateTimeKind kind);
new DateTime : int * int * int * int * int * int * int * int * DateTimeKind -> DateTime
Public Sub New (year As Integer, month As Integer, day As Integer, hour As Integer, minute As Integer, second As Integer, millisecond As Integer, microsecond As Integer, kind As DateTimeKind)
パラメーター
- 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
year、month、day、hour、minute、second、および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 より大きい値です。
kind は、 DateTimeKind 値の 1 つではありません。
注釈
このコンストラクターは、グレゴリオ暦の年、月、日として year、 month 、および day を解釈します。 別のカレンダーで年、月、日を使用して DateTime 値をインスタンス化するには、 DateTime(Int32, Int32, Int32, Int32, Int32, Int32, Int32, Int32, Calendar, DateTimeKind) コンストラクターを呼び出します。
日付と時刻のデータの移植性またはタイム ゾーン認識の程度が限られているアプリケーションでは、対応する DateTimeOffset コンストラクターを使用できます。
適用対象
DateTime(Int32, Int32, Int32, Int32, Int32, Int32, Int32, Calendar, DateTimeKind)
- ソース:
- DateTime.cs
- ソース:
- DateTime.cs
- ソース:
- 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);
public DateTime(int year, int month, int day, int hour, int minute, int second, int millisecond, System.Globalization.Calendar calendar, DateTimeKind kind);
new DateTime : int * int * int * int * int * int * int * System.Globalization.Calendar * DateTimeKind -> DateTime
Public Sub New (year As Integer, month As Integer, day As Integer, hour As Integer, minute As Integer, second As Integer, millisecond As Integer, calendar As Calendar, kind As DateTimeKind)
パラメーター
- 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
year、month、およびdayを解釈するために使用されるカレンダー。
- kind
- DateTimeKind
year、month、day、hour、minute、second、およびmillisecondがローカル時刻、世界協定時刻 (UTC)、またはどちらも指定されていないかどうかを示す列挙値の 1 つ。
例外
calendar は nullです。
year が、 calendarでサポートされている範囲内にありません。
-又は-
month が 1 未満か、 calendarの月数を超えている。
-又は-
day が 1 未満か、 monthの日数を超えています。
-又は-
hour が 0 より小さいか、23 より大きい値です。
-又は-
minute が 0 未満または 59 より大きい。
-又は-
second が 0 未満または 59 より大きい。
-又は-
millisecond が 0 より小さいか、999 より大きい値です。
kind は、 DateTimeKind 値の 1 つではありません。
例
次の例では、 DateTime(Int32, Int32, Int32, Int32, Int32, Int32, Int32, Calendar, DateTimeKind) コンストラクターを 2 回呼び出して、2 つの DateTime 値をインスタンス化します。 最初の呼び出しでは、PersianCalendar オブジェクトを使用してDateTime値がインスタンス化されます。 ペルシャ暦は文化の既定のカレンダーとして指定できないため、ペルシア暦に日付を表示するには、その PersianCalendar.GetMonth、 PersianCalendar.GetDayOfMonth、および PersianCalendar.GetYear メソッドを個別に呼び出す必要があります。 コンストラクターの 2 番目の呼び出しでは、HijriCalendar オブジェクトを使用してDateTime値をインスタンス化します。 この例では、現在のカルチャをアラビア語 (シリア) に変更し、現在のカルチャの既定のカレンダーをイスラム暦に変更します。 Hijri は現在のカルチャの既定のカレンダーであるため、 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
open System
open System.Globalization
open System.Text.RegularExpressions
open System.Threading
let getCalendarName (cal: Calendar) =
Regex.Match(string cal, "\\.(\\w+)Calendar").Groups[1].Value
printfn "Using the Persian Calendar:"
let persian = PersianCalendar()
let date1 = DateTime(1389, 5, 27, 16, 32, 18, 500, persian, DateTimeKind.Local)
printfn $"""{date1.ToString "M/dd/yyyy h:mm:ss.fff tt"} {date1.Kind}"""
let sep = DateTimeFormatInfo.CurrentInfo.TimeSeparator
printfn $"{persian.GetMonth date1}/{persian.GetDayOfMonth date1}/{persian.GetYear date1} {persian.GetHour date1}{sep}{persian.GetMinute date1:D2}{sep}{persian.GetSecond date1:D2}.{persian.GetMilliseconds date1:G3} {date1.Kind}\n"
printfn "Using the Hijri Calendar:"
// Get current culture so it can later be restored.
let dftCulture = Thread.CurrentThread.CurrentCulture
// Define Hijri calendar.
let hijri = HijriCalendar()
// Make ar-SY the current culture and Hijri the current calendar.
Thread.CurrentThread.CurrentCulture <- CultureInfo "ar-SY"
let current = CultureInfo.CurrentCulture
current.DateTimeFormat.Calendar <- hijri
let dFormat =
let dFormat = current.DateTimeFormat.ShortDatePattern
// Ensure year is displayed as four digits.
Regex.Replace(dFormat, "/yy$", "/yyyy") + " H:mm:ss.fff"
let fmtString = "{0} culture using the {1} calendar: {2:" + dFormat + "} {3}"
let date2 = 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
let dFormat2 = DateTimeFormatInfo.CurrentInfo.ShortDatePattern + " H:mm:ss.fff"
let fmtString2 = "{0} culture using the {1} calendar: {2:" + dFormat2 + "} {3}"
Console.WriteLine(fmtString2, CultureInfo.CurrentCulture, getCalendarName CultureInfo.CurrentCulture.Calendar, date2, date2.Kind)
// 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
Imports System.Globalization
Imports System.Text.RegularExpressions
Imports System.Threading
Module Example
Public Sub Main()
Console.WriteLine("Using the Persian Calendar:")
Dim persian As New PersianCalendar()
Dim date1 As New Date(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}", _
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()
Console.WriteLine("Using the Hijri Calendar:")
' Get current culture so it can later be restored.
Dim dftCulture As CultureInfo = Thread.CurrentThread.CurrentCulture
' Define strings for use in composite formatting.
Dim dFormat As String
Dim fmtString As String
' Define Hijri calendar.
Dim hijri As New HijriCalendar()
' Make ar-SY the current culture and Hijri the current calendar.
Thread.CurrentThread.CurrentCulture = New CultureInfo("ar-SY")
Dim current As CultureInfo = 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}"
Dim date2 As New Date(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)
End Sub
Private Function GetCalendarName(cal As Calendar) As String
Return Regex.Match(cal.ToString(), "\.(\w+)Calendar").Groups(1).Value
End Function
End Module
' The example displays the following output:
' Using the Persian Calendar:
' 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
注釈
year、month、およびdayパラメーターの許容値は、calendar パラメーターによって異なります。
calendarを使用して指定した日時を表すことができない場合は、例外がスローされます。
日付と時刻のデータの移植性またはタイム ゾーン認識の程度が限られているアプリケーションでは、対応する DateTimeOffset コンストラクターを使用できます。
Important
日本暦の年号は天皇の治世に基づいているため、変化することが期待されます。 たとえば、2019 年 5 月 1 日は、 JapaneseCalendar と JapaneseLunisolarCalendarのレイワ時代の始まりを迎えています。 このような時代の変化は、これらのカレンダーを使用するすべてのアプリケーションに影響します。 詳細およびアプリケーションが影響を受けるかどうかを判断するには、「 .NET の日本語カレンダーでの新しい時代 (年号) の処理」を参照してください。 Windows システムでアプリケーションをテストし、時代 (年号) の変更に対する準備を行う方法については、「 日本の時代 (年号) に合わせてアプリケーションを準備する」を参照してください。 複数の時代 (年号) を含むカレンダーをサポートする .NET の機能と、複数の時代 (年号) をサポートするカレンダーを使用する場合のベスト プラクティスについては、「 年号の使用」を参照してください。
System.Globalization名前空間には、GregorianCalendarやJulianCalendarなど、いくつかのカレンダーが用意されています。
適用対象
DateTime(Int32, Int32, Int32, Int32, Int32, Int32, Int32, Int32)
- ソース:
- DateTime.cs
- ソース:
- DateTime.cs
- ソース:
- 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);
public DateTime(int year, int month, int day, int hour, int minute, int second, int millisecond, int microsecond);
new DateTime : int * int * int * int * int * int * int * int -> DateTime
Public Sub New (year As Integer, month As Integer, day As Integer, hour As Integer, minute As Integer, second As Integer, millisecond As Integer, microsecond As Integer)
パラメーター
- 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 コンストラクターを使用できます。
適用対象
DateTime(Int32, Int32, Int32, Int32, Int32, Int32, Int32, Calendar)
- ソース:
- DateTime.cs
- ソース:
- DateTime.cs
- ソース:
- 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);
public DateTime(int year, int month, int day, int hour, int minute, int second, int millisecond, System.Globalization.Calendar calendar);
new DateTime : int * int * int * int * int * int * int * System.Globalization.Calendar -> DateTime
Public Sub New (year As Integer, month As Integer, day As Integer, hour As Integer, minute As Integer, second As Integer, millisecond As Integer, calendar As 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
year、month、およびdayを解釈するために使用されるカレンダー。
例外
calendar は nullです。
year が、 calendarでサポートされている範囲内にありません。
-又は-
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 値をインスタンス化します。 最初の呼び出しでは、PersianCalendar オブジェクトを使用してDateTime値がインスタンス化されます。 ペルシャ暦は文化の既定のカレンダーとして指定できないため、ペルシア暦に日付を表示するには、その PersianCalendar.GetMonth、 PersianCalendar.GetDayOfMonth、および PersianCalendar.GetYear メソッドを個別に呼び出す必要があります。 コンストラクターの 2 番目の呼び出しでは、HijriCalendar オブジェクトを使用してDateTime値をインスタンス化します。 この例では、現在のカルチャをアラビア語 (シリア) に変更し、現在のカルチャの既定のカレンダーをイスラム暦に変更します。 Hijri は現在のカルチャの既定のカレンダーであるため、 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
open System
open System.Globalization
open System.Text.RegularExpressions
open System.Threading
let getCalendarName (cal: Calendar) =
Regex.Match(string cal, "\\.(\\w+)Calendar").Groups[1].Value
printfn "Using the Persian Calendar:"
let persian = PersianCalendar()
let date1 = DateTime(1389, 5, 27, 16, 32, 18, 500, persian)
printfn $"""{date1.ToString("M/dd/yyyy h:mm:ss.fff tt")}"""
let sep = DateTimeFormatInfo.CurrentInfo.TimeSeparator
printfn $"{persian.GetMonth date1}/{persian.GetDayOfMonth date1}/{persian.GetYear date1} {persian.GetHour date1}{sep}%02i{persian.GetMinute date1}{sep}%02i{persian.GetSecond date1}.%.3f{persian.GetMilliseconds date1}\n"
printfn "Using the Hijri Calendar:"
// Get current culture so it can later be restored.
let dftCulture = Thread.CurrentThread.CurrentCulture
// Define Hijri calendar.
let hijri = HijriCalendar()
// Make ar-SY the current culture and Hijri the current calendar.
Thread.CurrentThread.CurrentCulture <- CultureInfo "ar-SY"
let current = CultureInfo.CurrentCulture
current.DateTimeFormat.Calendar <- hijri
let dFormat =
let dFormat = current.DateTimeFormat.ShortDatePattern
// Ensure year is displayed as four digits.
Regex.Replace(dFormat, "/yy$", "/yyyy") + " H:mm:ss.fff"
let fmtString = "{0} culture using the {1} calendar: {2:" + dFormat + "}"
let date2 = DateTime(1431, 9, 9, 16, 32, 18, 500, hijri)
Console.WriteLine(fmtString, current, getCalendarName hijri, date2)
// Restore previous culture.
Thread.CurrentThread.CurrentCulture <- dftCulture
let dFormat2 = DateTimeFormatInfo.CurrentInfo.ShortDatePattern + " H:mm:ss.fff"
let fmtString2 = "{0} culture using the {1} calendar: {2:" + dFormat + "}"
Console.WriteLine(fmtString2, CultureInfo.CurrentCulture, getCalendarName CultureInfo.CurrentCulture.Calendar, date2)
// 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
Imports System.Globalization
Imports System.Text.RegularExpressions
Imports System.Threading
Module Example
Public Sub Main()
Console.WriteLine("Using the Persian Calendar:")
Dim persian As New PersianCalendar()
Dim date1 As New Date(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}", _
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()
Console.WriteLine("Using the Hijri Calendar:")
' Get current culture so it can later be restored.
Dim dftCulture As CultureInfo = Thread.CurrentThread.CurrentCulture
' Define strings for use in composite formatting.
Dim dFormat As String
Dim fmtString As String
' Define Hijri calendar.
Dim hijri As New HijriCalendar()
' Make ar-SY the current culture and Hijri the current calendar.
Thread.CurrentThread.CurrentCulture = New CultureInfo("ar-SY")
Dim current As CultureInfo = 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 + "}"
Dim date2 As New Date(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)
End Sub
Private Function GetCalendarName(cal As Calendar) As String
Return Regex.Match(cal.ToString(), "\.(\w+)Calendar").Groups(1).Value
End Function
End Module
' The example displays the following output:
' Using the Persian Calendar:
' 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に初期化されます。
year、month、およびdayの許容値は、calendarによって異なります。
calendarを使用して指定した日時を表すことができない場合は、例外がスローされます。
日付と時刻のデータの移植性またはタイム ゾーン認識の程度が限られているアプリケーションでは、対応する DateTimeOffset コンストラクターを使用できます。
Important
日本暦の年号は天皇の治世に基づいているため、変化することが期待されます。 たとえば、2019 年 5 月 1 日は、 JapaneseCalendar と JapaneseLunisolarCalendarのレイワ時代の始まりを迎えています。 このような時代の変化は、これらのカレンダーを使用するすべてのアプリケーションに影響します。 詳細およびアプリケーションが影響を受けるかどうかを判断するには、「 .NET の日本語カレンダーでの新しい時代 (年号) の処理」を参照してください。 Windows システムでアプリケーションをテストし、時代 (年号) の変更に対する準備を行う方法については、「 日本の時代 (年号) に合わせてアプリケーションを準備する」を参照してください。 複数の時代 (年号) を含むカレンダーをサポートする .NET の機能と、複数の時代 (年号) をサポートするカレンダーを使用する場合のベスト プラクティスについては、「 年号の使用」を参照してください。
System.Globalization名前空間には、GregorianCalendarやJulianCalendarなど、いくつかのカレンダーが用意されています。
こちらもご覧ください
適用対象
DateTime(Int32, Int32, Int32, Int32, Int32, Int32, Int32, DateTimeKind)
- ソース:
- DateTime.cs
- ソース:
- DateTime.cs
- ソース:
- DateTime.cs
- ソース:
- DateTime.cs
- ソース:
- DateTime.cs
指定した年、月、日、時、分、秒、ミリ秒、協定世界時 (UTC) または現地時刻に DateTime 構造体の新しいインスタンスを初期化します。
public:
DateTime(int year, int month, int day, int hour, int minute, int second, int millisecond, DateTimeKind kind);
public DateTime(int year, int month, int day, int hour, int minute, int second, int millisecond, DateTimeKind kind);
new DateTime : int * int * int * int * int * int * int * DateTimeKind -> DateTime
Public Sub New (year As Integer, month As Integer, day As Integer, hour As Integer, minute As Integer, second As Integer, millisecond As Integer, kind As DateTimeKind)
パラメーター
- 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
year、month、day、hour、minute、second、およびmillisecondがローカル時刻、世界協定時刻 (UTC)、またはどちらも指定されていないかどうかを示す列挙値の 1 つ。
例外
year が 1 より小さいか、9999 より大きい。
-又は-
month が 1 より小さいか、12 より大きい。
-又は-
day が 1 未満か、 monthの日数を超えています。
-又は-
hour が 0 より小さいか、23 より大きい値です。
-又は-
minute が 0 未満または 59 より大きい。
-又は-
second が 0 未満または 59 より大きい。
-又は-
millisecond が 0 より小さいか、999 より大きい値です。
kind は、 DateTimeKind 値の 1 つではありません。
例
次の例では、 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
let date1 = DateTime(2010, 8, 18, 16, 32, 18, 500, DateTimeKind.Local)
printfn $"""{date1.ToString "M/dd/yyyy h:mm:ss.fff tt"} {date1.Kind}"""
// The example displays the following output, in this case for en-us culture:
// 8/18/2010 4:32:18.500 PM Local
Dim date1 As New Date(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:
' 8/18/2010 4:32:18.500 PM Local
注釈
このコンストラクターはyear、 month、および day をグレゴリオ暦の年、月、日として解釈します。 別のカレンダーで年、月、日を使用して DateTime 値をインスタンス化するには、 DateTime(Int32, Int32, Int32, Int32, Int32, Int32, Int32, Calendar, DateTimeKind) コンストラクターを呼び出します。
日付と時刻のデータの移植性またはタイム ゾーン認識の程度が限られているアプリケーションでは、対応する DateTimeOffset コンストラクターを使用できます。
適用対象
DateTime(Int32, Int32, Int32, Int32, Int32, Int32, Int32)
- ソース:
- DateTime.cs
- ソース:
- DateTime.cs
- ソース:
- DateTime.cs
- ソース:
- DateTime.cs
- ソース:
- DateTime.cs
指定した年、月、日、時、分、秒、ミリ秒に DateTime 構造体の新しいインスタンスを初期化します。
public:
DateTime(int year, int month, int day, int hour, int minute, int second, int millisecond);
public DateTime(int year, int month, int day, int hour, int minute, int second, int millisecond);
new DateTime : int * int * int * int * int * int * int -> DateTime
Public Sub New (year As Integer, month As Integer, day As Integer, hour As Integer, minute As Integer, second As Integer, millisecond As Integer)
パラメーター
- 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
let date1 = DateTime(2010, 8, 18, 16, 32, 18, 500)
date1.ToString "M/dd/yyyy h:mm:ss.fff tt"
|> printfn "%s"
// The example displays the following output, in this case for en-us culture:
// 8/18/2010 4:32:18.500 PM
Dim date1 As New Date(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:
' 8/18/2010 4:32:18.500 PM
注釈
このコンストラクターはyear、 month、および day をグレゴリオ暦の年、月、日として解釈します。 別のカレンダーで年、月、日を使用して DateTime 値をインスタンス化するには、 DateTime(Int32, Int32, Int32, Int32, Int32, Int32, Int32, Calendar) コンストラクターを呼び出します。
Kind プロパティは、Unspecifiedに初期化されます。
日付と時刻のデータの移植性またはタイム ゾーン認識の程度が限られているアプリケーションでは、対応する DateTimeOffset コンストラクターを使用できます。
こちらもご覧ください
適用対象
DateTime(Int32, Int32, Int32, Int32, Int32, Int32, Int32, Int32, Calendar, DateTimeKind)
- ソース:
- DateTime.cs
- ソース:
- DateTime.cs
- ソース:
- 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);
public DateTime(int year, int month, int day, int hour, int minute, int second, int millisecond, int microsecond, System.Globalization.Calendar calendar, DateTimeKind kind);
new DateTime : int * int * int * int * int * int * int * int * System.Globalization.Calendar * DateTimeKind -> DateTime
Public Sub New (year As Integer, month As Integer, day As Integer, hour As Integer, minute As Integer, second As Integer, millisecond As Integer, microsecond As Integer, calendar As Calendar, kind As DateTimeKind)
パラメーター
- 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
year、month、およびdayを解釈するために使用されるカレンダー。
- kind
- DateTimeKind
year、month、day、hour、minute、second、およびmillisecondがローカル時刻、世界協定時刻 (UTC)、またはどちらも指定されていないかどうかを示す列挙値の 1 つ。
例外
calendar は null です
year が、 calendarでサポートされている範囲内にありません。
-又は-
month が 1 未満か、 calendarの月数を超えている。
-又は-
day が 1 未満か、 monthの日数を超えています。
-又は-
hour が 0 より小さいか、23 より大きい値です。
-又は-
minute が 0 未満または 59 より大きい。
-又は-
second が 0 未満または 59 より大きい。
-又は-
millisecond が 0 より小さいか、999 より大きい値です。
-又は-
microsecond が 0 より小さいか、999 より大きい値です。
kind は、 DateTimeKind 値の 1 つではありません。
注釈
year、month、およびdayパラメーターの許容値は、calendar パラメーターによって異なります。
calendarを使用して指定した日時を表すことができない場合は、例外がスローされます。
日付と時刻のデータの移植性またはタイム ゾーン認識の程度が限られているアプリケーションでは、対応する DateTimeOffset コンストラクターを使用できます。
適用対象
DateTime(Int32, Int32, Int32, Int32, Int32, Int32, DateTimeKind)
- ソース:
- DateTime.cs
- ソース:
- DateTime.cs
- ソース:
- DateTime.cs
- ソース:
- DateTime.cs
- ソース:
- DateTime.cs
指定した年、月、日、時、分、秒、協定世界時 (UTC) または現地時刻に DateTime 構造体の新しいインスタンスを初期化します。
public:
DateTime(int year, int month, int day, int hour, int minute, int second, DateTimeKind kind);
public DateTime(int year, int month, int day, int hour, int minute, int second, DateTimeKind kind);
new DateTime : int * int * int * int * int * int * DateTimeKind -> DateTime
Public Sub New (year As Integer, month As Integer, day As Integer, hour As Integer, minute As Integer, second As Integer, kind As DateTimeKind)
パラメーター
- 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
year、month、day、hour、minute、およびsecondがローカル時刻、世界協定時刻 (UTC)、またはどちらも指定されていないかどうかを示す列挙値の 1 つ。
例外
year が 1 より小さいか、9999 より大きい。
-又は-
month が 1 より小さいか、12 より大きい。
-又は-
day が 1 未満か、 monthの日数を超えています。
-又は-
hour が 0 より小さいか、23 より大きい値です。
-又は-
minute が 0 未満または 59 より大きい。
-又は-
second が 0 未満または 59 より大きい。
kind は、 DateTimeKind 値の 1 つではありません。
例
次の例では、 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
let date1 = DateTime(2010, 8, 18, 16, 32, 0, DateTimeKind.Local)
printfn $"{date1} {date1.Kind}"
// The example displays the following output, in this case for en-us culture:
// 8/18/2010 4:32:00 PM Local
Dim date1 As New Date(2010, 8, 18, 16, 32, 0, DateTimeKind.Local)
Console.WriteLine("{0} {1}", date1, date1.Kind)
' The example displays the following output:
' 8/18/2010 4:32:00 PM Local
注釈
このコンストラクターはyear、 month、および day をグレゴリオ暦の年、月、日として解釈します。 別のカレンダーで年、月、日を使用して DateTime 値をインスタンス化するには、 DateTime(Int32, Int32, Int32, Int32, Int32, Int32, Int32, Calendar, DateTimeKind) コンストラクターを呼び出します。
日付と時刻のデータの移植性またはタイム ゾーン認識の程度が限られているアプリケーションでは、対応する DateTimeOffset コンストラクターを使用できます。
適用対象
DateTime(Int32, Int32, Int32, Int32, Int32, Int32)
- ソース:
- DateTime.cs
- ソース:
- DateTime.cs
- ソース:
- DateTime.cs
- ソース:
- DateTime.cs
- ソース:
- DateTime.cs
指定した年、月、日、時、分、秒に DateTime 構造体の新しいインスタンスを初期化します。
public:
DateTime(int year, int month, int day, int hour, int minute, int second);
public DateTime(int year, int month, int day, int hour, int minute, int second);
new DateTime : int * int * int * int * int * int -> DateTime
Public Sub New (year As Integer, month As Integer, day As Integer, hour As Integer, minute As Integer, second As Integer)
パラメーター
- 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
let date1 = DateTime(2010, 8, 18, 16, 32, 0)
printfn $"{date1}"
// The example displays the following output, in this case for en-us culture:
// 8/18/2010 4:32:00 PM
Dim date1 As New Date(2010, 8, 18, 16, 32, 0)
Console.WriteLine(date1.ToString())
' The example displays the following output:
' 8/18/2010 4:32:00 PM
注釈
Kind プロパティは、Unspecifiedに初期化されます。
このコンストラクターはyear、 month、および day をグレゴリオ暦の年、月、日として解釈します。 別のカレンダーで年、月、日を使用して DateTime 値をインスタンス化するには、 DateTime(Int32, Int32, Int32, Int32, Int32, Int32, Calendar) コンストラクターを呼び出します。
日付と時刻のデータの移植性またはタイム ゾーン認識の程度が限られているアプリケーションでは、対応する DateTimeOffset コンストラクターを使用できます。
適用対象
DateTime(Int32, Int32, Int32, Calendar)
- ソース:
- DateTime.cs
- ソース:
- DateTime.cs
- ソース:
- DateTime.cs
- ソース:
- DateTime.cs
- ソース:
- DateTime.cs
DateTime構造体の新しいインスタンスを、指定したカレンダーの指定した年、月、日に初期化します。
public:
DateTime(int year, int month, int day, System::Globalization::Calendar ^ calendar);
public DateTime(int year, int month, int day, System.Globalization.Calendar calendar);
new DateTime : int * int * int * System.Globalization.Calendar -> DateTime
Public Sub New (year As Integer, month As Integer, day As Integer, calendar As Calendar)
パラメーター
- year
- Int32
年 (1 ~ calendar年)。
- month
- Int32
月 (1 ~ calendarの月数)。
- day
- Int32
日 (1 から month の日数)。
- calendar
- Calendar
year、month、およびdayを解釈するために使用されるカレンダー。
例外
calendar は nullです。
year が、 calendarでサポートされている範囲内にありません。
-又は-
month が 1 未満か、 calendarの月数を超えている。
-又は-
day が 1 未満か、 monthの日数を超えています。
例
次の例では、 DateTime(Int32, Int32, Int32, Calendar) コンストラクターを 2 回呼び出して、2 つの DateTime 値をインスタンス化します。 最初の呼び出しでは、PersianCalendar オブジェクトを使用してDateTime値がインスタンス化されます。 ペルシャ暦は文化の既定のカレンダーとして指定できないため、ペルシア暦に日付を表示するには、その PersianCalendar.GetMonth、 PersianCalendar.GetDayOfMonth、および PersianCalendar.GetYear メソッドを個別に呼び出す必要があります。 コンストラクターの 2 番目の呼び出しでは、HijriCalendar オブジェクトを使用してDateTime値をインスタンス化します。 この例では、現在のカルチャをアラビア語 (シリア) に変更し、現在のカルチャの既定のカレンダーをイスラム暦に変更します。 Hijri は現在のカルチャの既定のカレンダーであるため、 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
open System
open System.Globalization
open System.Text.RegularExpressions
open System.Threading
let getCalendarName (cal: Calendar) =
Regex.Match(string cal, "\\.(\\w+)Calendar").Groups[1].Value
printfn "Using the Persian Calendar:"
let persian = PersianCalendar()
let date1 = DateTime(1389, 5, 27, persian)
printfn $"{date1}"
printfn $"{persian.GetMonth date1}/{persian.GetDayOfMonth date1}/{persian.GetYear date1}\n"
printfn "Using the Hijri Calendar:"
// Get current culture so it can later be restored.
let dftCulture = Thread.CurrentThread.CurrentCulture
// Define Hijri calendar.
let hijri = HijriCalendar()
// Make ar-SY the current culture and Hijri the current calendar.
Thread.CurrentThread.CurrentCulture <- CultureInfo "ar-SY"
let current = CultureInfo.CurrentCulture
current.DateTimeFormat.Calendar <- hijri
let dFormat =
let dFormat = current.DateTimeFormat.ShortDatePattern
// Ensure year is displayed as four digits.
Regex.Replace(dFormat, "/yy$", "/yyyy")
current.DateTimeFormat.ShortDatePattern <- dFormat
let date2 = DateTime(1431, 9, 9, hijri)
printfn $"{current} culture using the {getCalendarName hijri} calendar: {date2:d}"
// Restore previous culture.
Thread.CurrentThread.CurrentCulture <- dftCulture
printfn $"{CultureInfo.CurrentCulture} culture using the {getCalendarName CultureInfo.CurrentCulture.Calendar} calendar: {date2:d}"
// 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
Imports System.Globalization
Imports System.Text.RegularExpressions
Imports System.Threading
Module Example
Public Sub Main()
Console.WriteLine("Using the Persian Calendar:")
Dim persian As New PersianCalendar()
Dim date1 As New Date(1389, 5, 27, persian)
Console.WriteLine(date1.ToString())
Console.WriteLine("{0}/{1}/{2}", persian.GetMonth(date1), _
persian.GetDayOfMonth(date1), _
persian.GetYear(date1))
Console.WriteLine()
Console.WriteLine("Using the Hijri Calendar:")
' Get current culture so it can later be restored.
Dim dftCulture As CultureInfo = Thread.CurrentThread.CurrentCulture
' Define Hijri calendar.
Dim hijri As New HijriCalendar()
' Make ar-SY the current culture and Hijri the current calendar.
Thread.CurrentThread.CurrentCulture = New CultureInfo("ar-SY")
Dim current As CultureInfo = CultureInfo.CurrentCulture
current.DateTimeFormat.Calendar = hijri
Dim dFormat As String = current.DateTimeFormat.ShortDatePattern
' Ensure year is displayed as four digits.
dFormat = Regex.Replace(dFormat, "/yy$", "/yyyy")
current.DateTimeFormat.ShortDatePattern = dFormat
Dim date2 As New Date(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)
End Sub
Private Function GetCalendarName(cal As Calendar) As String
Return Regex.Match(cal.ToString(), "\.(\w+)Calendar").Groups(1).Value
End Function
End Module
' 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に初期化されます。
year、month、およびdayの許容値は、calendarによって異なります。
calendarを使用して指定した日時を表すことができない場合は、例外がスローされます。
Important
日本暦の年号は天皇の治世に基づいているため、変化することが期待されます。 たとえば、2019 年 5 月 1 日は、 JapaneseCalendar と JapaneseLunisolarCalendarのレイワ時代の始まりを迎えています。 このような時代の変化は、これらのカレンダーを使用するすべてのアプリケーションに影響します。 詳細およびアプリケーションが影響を受けるかどうかを判断するには、「 .NET の日本語カレンダーでの新しい時代 (年号) の処理」を参照してください。 Windows システムでアプリケーションをテストし、時代 (年号) の変更に対する準備を行う方法については、「 日本の時代 (年号) に合わせてアプリケーションを準備する」を参照してください。 複数の時代 (年号) を含むカレンダーをサポートする .NET の機能と、複数の時代 (年号) をサポートするカレンダーを使用する場合のベスト プラクティスについては、「 年号の使用」を参照してください。
System.Globalization名前空間には、GregorianCalendarやJulianCalendarなど、いくつかのカレンダーが用意されています。
こちらもご覧ください
適用対象
DateTime(Int32, Int32, Int32)
- ソース:
- DateTime.cs
- ソース:
- DateTime.cs
- ソース:
- DateTime.cs
- ソース:
- DateTime.cs
- ソース:
- DateTime.cs
指定した年、月、日に DateTime 構造体の新しいインスタンスを初期化します。
public:
DateTime(int year, int month, int day);
public DateTime(int year, int month, int day);
new DateTime : int * int * int -> DateTime
Public Sub New (year As Integer, month As Integer, day As Integer)
パラメーター
- 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
let date1 = DateTime(2010, 8, 18)
printfn $"{date1}"
// The example displays the following output:
// 8/18/2010 12:00:00 AM
Dim date1 As New Date(2010, 8, 18)
Console.WriteLine(date1.ToString())
' The example displays the following output:
' 8/18/2010 12:00:00 AM
注釈
このコンストラクターは、グレゴリオ暦の年、月、日として year、 month、および day を解釈します。 別のカレンダーで年、月、日を使用して DateTime 値をインスタンス化するには、 DateTime(Int32, Int32, Int32, Calendar) コンストラクターを呼び出します。
結果の DateTime の時刻は午前 0 時 (00:00:00) です。 Kind プロパティは、DateTimeKind.Unspecifiedに初期化されます。
適用対象
DateTime(DateOnly, TimeOnly, DateTimeKind)
- ソース:
- DateTime.cs
- ソース:
- DateTime.cs
- ソース:
- DateTime.cs
- ソース:
- DateTime.cs
指定したDateOnlyにDateTime構造体の新しいインスタンスを初期化し、指定したDateTimeKindを考慮してTimeOnlyします。
public:
DateTime(DateOnly date, TimeOnly time, DateTimeKind kind);
public DateTime(DateOnly date, TimeOnly time, DateTimeKind kind);
new DateTime : DateOnly * TimeOnly * DateTimeKind -> DateTime
Public Sub New (date As DateOnly, time As TimeOnly, kind As DateTimeKind)
パラメーター
- date
- DateOnly
日付部分。
- time
- TimeOnly
時間部分。
- kind
- DateTimeKind
dateとtimeが現地時刻、協定世界時 (UTC)、またはどちらも指定していないかどうかを示す列挙値の 1 つ。
適用対象
DateTime(Int64, DateTimeKind)
- ソース:
- DateTime.cs
- ソース:
- DateTime.cs
- ソース:
- DateTime.cs
- ソース:
- DateTime.cs
- ソース:
- DateTime.cs
DateTime構造体の新しいインスタンスを、指定したティック数と協定世界時 (UTC) または現地時刻に初期化します。
public:
DateTime(long ticks, DateTimeKind kind);
public DateTime(long ticks, DateTimeKind kind);
new DateTime : int64 * DateTimeKind -> DateTime
Public Sub New (ticks As Long, kind As DateTimeKind)
パラメーター
- ticks
- Int64
グレゴリオ暦の 0001 年 1 月 1 日 00:00:00.000 から経過した 100 ナノ秒間隔の数で表される日付と時刻。
- kind
- DateTimeKind
ローカル時刻、協定世界時 (UTC)、またはどちらも指定 ticks かどうかを示す列挙値の 1 つ。
例外
ticks が DateTime.MinValue より小さいか、 DateTime.MaxValue より大きい値です。
kind は、 DateTimeKind 値の 1 つではありません。
注釈
日付と時刻のデータの移植性またはタイム ゾーン認識の程度が限られているアプリケーションでは、対応する DateTimeOffset コンストラクターを使用できます。
適用対象
DateTime(DateOnly, TimeOnly)
- ソース:
- DateTime.cs
- ソース:
- DateTime.cs
- ソース:
- DateTime.cs
- ソース:
- DateTime.cs
指定したDateOnlyとTimeOnlyにDateTime構造体の新しいインスタンスを初期化します。 新しいインスタンスには、 Unspecified の種類があります。
public:
DateTime(DateOnly date, TimeOnly time);
public DateTime(DateOnly date, TimeOnly time);
new DateTime : DateOnly * TimeOnly -> DateTime
Public Sub New (date As DateOnly, time As TimeOnly)
パラメーター
- date
- DateOnly
日付部分。
- time
- TimeOnly
時間部分。
適用対象
DateTime(Int32, Int32, Int32, Int32, Int32, Int32, Calendar)
- ソース:
- DateTime.cs
- ソース:
- DateTime.cs
- ソース:
- 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);
public DateTime(int year, int month, int day, int hour, int minute, int second, System.Globalization.Calendar calendar);
new DateTime : int * int * int * int * int * int * System.Globalization.Calendar -> DateTime
Public Sub New (year As Integer, month As Integer, day As Integer, hour As Integer, minute As Integer, second As Integer, calendar As 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
year、month、およびdayを解釈するために使用されるカレンダー。
例外
calendar は nullです。
year が、 calendarでサポートされている範囲内にありません。
-又は-
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 値をインスタンス化します。 最初の呼び出しでは、PersianCalendar オブジェクトを使用してDateTime値がインスタンス化されます。 ペルシャ暦は文化の既定のカレンダーとして指定できないため、ペルシア暦に日付を表示するには、その PersianCalendar.GetMonth、 PersianCalendar.GetDayOfMonth、および PersianCalendar.GetYear メソッドを個別に呼び出す必要があります。 コンストラクターの 2 番目の呼び出しでは、HijriCalendar オブジェクトを使用してDateTime値をインスタンス化します。 この例では、現在のカルチャをアラビア語 (シリア) に変更し、現在のカルチャの既定のカレンダーをイスラム暦に変更します。 Hijri は現在のカルチャの既定のカレンダーであるため、 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
open System
open System.Globalization
open System.Text.RegularExpressions
open System.Threading
let getCalendarName (cal: Calendar) =
Regex.Match(string cal, "\\.(\\w+)Calendar").Groups[1].Value
printfn "Using the Persian Calendar:"
let persian = PersianCalendar()
let date1 = DateTime(1389, 5, 27, 16, 32, 0, persian)
printfn $"{date1}"
let sep = DateTimeFormatInfo.CurrentInfo.TimeSeparator
printfn $"{persian.GetMonth date1}/{persian.GetDayOfMonth date1}/{persian.GetYear date1} {persian.GetHour date1}{sep}%02i{persian.GetMinute date1}{sep}%02i{persian.GetSecond date1}\n"
printfn "Using the Hijri Calendar:"
// Get current culture so it can later be restored.
let dftCulture = Thread.CurrentThread.CurrentCulture
// Define Hijri calendar.
let hijri = HijriCalendar()
// Make ar-SY the current culture and Hijri the current calendar.
Thread.CurrentThread.CurrentCulture <- CultureInfo "ar-SY"
let current = CultureInfo.CurrentCulture
current.DateTimeFormat.Calendar <- hijri
let dFormat =
let dFormat = current.DateTimeFormat.ShortDatePattern
// Ensure year is displayed as four digits.
Regex.Replace(dFormat, "/yy$", "/yyyy")
current.DateTimeFormat.ShortDatePattern <- dFormat
let date2 = DateTime(1431, 9, 9, 16, 32, 18, hijri)
printfn $"{current} culture using the {getCalendarName hijri} calendar: {date2:g}"
// Restore previous culture.
Thread.CurrentThread.CurrentCulture <- dftCulture
printfn $"{CultureInfo.CurrentCulture} culture using the {getCalendarName CultureInfo.CurrentCulture.Calendar} calendar: {date2:g}"
// 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
Imports System.Globalization
Imports System.Text.RegularExpressions
Imports System.Threading
Module Example
Public Sub Main()
Console.WriteLine("Using the Persian Calendar:")
Dim persian As New PersianCalendar()
Dim date1 As New Date(1389, 5, 27, 16, 32, 0, persian)
Console.WriteLine(date1.ToString())
Console.WriteLine("{0}/{1}/{2} {3}{6}{4:D2}{6}{5:D2}", persian.GetMonth(date1), _
persian.GetDayOfMonth(date1), _
persian.GetYear(date1), _
persian.GetHour(date1), _
persian.GetMinute(date1), _
persian.GetSecond(date1), _
DateTimeFormatInfo.CurrentInfo.TimeSeparator)
Console.WriteLine()
Console.WriteLine("Using the Hijri Calendar:")
' Get current culture so it can later be restored.
Dim dftCulture As CultureInfo = Thread.CurrentThread.CurrentCulture
' Define Hijri calendar.
Dim hijri As New HijriCalendar()
' Make ar-SY the current culture and Hijri the current calendar.
Thread.CurrentThread.CurrentCulture = New CultureInfo("ar-SY")
Dim current As CultureInfo = CultureInfo.CurrentCulture
current.DateTimeFormat.Calendar = hijri
Dim dFormat As String = current.DateTimeFormat.ShortDatePattern
' Ensure year is displayed as four digits.
dFormat = Regex.Replace(dFormat, "/yy$", "/yyyy")
current.DateTimeFormat.ShortDatePattern = dFormat
Dim date2 As New Date(1431, 9, 9, 16, 32, 0, 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)
End Sub
Private Function GetCalendarName(cal As Calendar) As String
Return Regex.Match(cal.ToString(), "\.(\w+)Calendar").Groups(1).Value
End Function
End Module
' 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に初期化されます。
year、month、およびdayの許容値は、calendarによって異なります。
calendarを使用して指定した日時を表すことができない場合は、例外がスローされます。
Important
日本暦の年号は天皇の治世に基づいているため、変化することが期待されます。 たとえば、2019 年 5 月 1 日は、 JapaneseCalendar と JapaneseLunisolarCalendarのレイワ時代の始まりを迎えています。 このような時代の変化は、これらのカレンダーを使用するすべてのアプリケーションに影響します。 詳細およびアプリケーションが影響を受けるかどうかを判断するには、「 .NET の日本語カレンダーでの新しい時代 (年号) の処理」を参照してください。 Windows システムでアプリケーションをテストし、時代 (年号) の変更に対する準備を行う方法については、「 日本の時代 (年号) に合わせてアプリケーションを準備する」を参照してください。 複数の時代 (年号) を含むカレンダーをサポートする .NET の機能と、複数の時代 (年号) をサポートするカレンダーを使用する場合のベスト プラクティスについては、「 年号の使用」を参照してください。
System.Globalization名前空間には、GregorianCalendarやJulianCalendarなど、いくつかのカレンダーが用意されています。