C# の日時

WBOY
リリース: 2024-09-03 15:16:12
オリジナル
281 人が閲覧しました

C# では、DateTime は構造体です。したがって、これは値型であり、時刻の瞬間を表すために使用されます。日付と時刻を表すために使用されます。 DateTime 型の値の範囲は、0001 年 1 月 1 日の午前 12:00:00 から西暦 9999 年 12 月 31 日の午後 11:59:59 までです。DateTime の値は値型であるため、null にすることはできません。 DateTime 値を初期化するには、DateTime コンストラクターのオーバーロードのいずれかを呼び出すことができます。プロパティまたはメソッドから返された値を DateTime オブジェクトに割り当てることもできます。

構文:

以下は、DateTime 構造体の新しいインスタンスを初期化する構文です。

DateTime date_time = new DateTime();
ログイン後にコピー

ここで、date_time は、DateTime 型のインスタンスに与えられたユーザー定義の名前です。 「new」演算子を使用してこのインスタンスを初期化しました。上記の構文では、暗黙的なパラメーターなしのコンストラクターを使用して、DateTime をデフォルト値に初期化しています。 DateTime コンストラクターのオーバーロードを使用して DateTime インスタンスを初期化することもできます。

C# では DateTime はどのように機能しますか?

C# では、いくつかの方法で DateTime を操作し、DateTime 変数に値を割り当てることができます。

  • 次に示すように、DateTime コンストラクターのオーバーロード (パラメーターなしのコンストラクターまたはパラメーター付きのコンストラクターのいずれか) を呼び出すことで、DateTime 変数に値を割り当てることができます。
DateTime dateTime = new DateTime(2020, 2, 8, 7, 32, 56);
ログイン後にコピー

上記のステートメントは、特定の年、月、日、時、分、秒の DateTime 構造体の新しいインスタンスを初期化します。

  • ここでは、以下の DateTime 構造体のコンストラクターを使用しました。
public DateTime(int year, int month, int day, int hour, int minute, int second);
ログイン後にコピー
  • 上記で説明したものとは別に、DateTime を操作するために利用できる他のコンストラクターが次の 10 個あります。
public DateTime(long ticks);
ログイン後にコピー
  • ここで、「ティック」は、グレゴリオ暦の 0001 年 1 月 1 日 00:00:00.000 から経過した 100 ナノ秒間隔の数で表される日付と時刻を表します。
public DateTime(long ticks, DateTimeKind kind);
ログイン後にコピー
  • ここで、「kind」は列挙値の中の値を表し、ティックが現地時間を指定するのか、協定世界時を指定するのか、または上記のいずれも指定しないのかを表します。
public DateTime(int year, int month, int day);
ログイン後にコピー
  • このコンテキストでは、「年」は 1 ~ 9999 の値を表し、「月」は 1 ~ 12 の値を表し、「日」は特定の月の日数の範囲内の値を表します。
public DateTime(int year, int month, int day, Calendar calendar);
ログイン後にコピー
  • ここで、「calendar」は年、月、日を解釈するために使用されるカレンダーを表します。
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, Calendar calendar);
ログイン後にコピー
public DateTime(int year, int month, int day, int hour, int minute, int second, int millisecond);
ログイン後にコピー
  • ここで、「ミリ秒」は 0 から 999 までのミリ秒を表します。
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, Calendar calendar);
ログイン後にコピー
public DateTime(int year, int month, int day, int hour, int minute, int second, int millisecond, Calendar calendar, DateTimeKind kind);
ログイン後にコピー
  • 以下に示すように、プロパティまたはメソッドから返された DateTime 値を DateTime 変数に割り当てることができます。
DateTime dateTime = DateTime.Now;
ログイン後にコピー

これにより、現在の日付と時刻が DateTime 変数に割り当てられます。

  • 次に示すように、文字列表現から DateTime 値を解析し、DateTime 変数に割り当てることができます。
string str = "6/2/2020 9:20:40 AM";
DateTime dateTime = DateTime.Parse(str, System.Globalization.CultureInfo.InvariantCulture);
ログイン後にコピー

上記の変換は、Parse()、ParseExact()、TryParse()、および TryParseExact() メソッドを使用して実行できます。

C# での DateTime の例

文字列を解析して DateTime オブジェクトにする方法の例をいくつか示します。

例 #1

DateTime によって提供されるプロパティとメソッドを使用して、現在の日付と時刻と明日の日付と時刻を表示する例:

コード:

using System;
using System.IO;
namespace ConsoleApp4
{
class Program
{
public static DateTime GetNextDay()
{
//getting next day using AddDays() method
return DateTime.Now.AddDays(1);
}
public static void Main()
{
//displaying current date and time using 'Now' property of DateTime
Console.WriteLine("Current date and time: {0}", DateTime.Now);
DateTime dateTime = GetNextDay();
Console.WriteLine("Tomorrow date and time: {0}", dateTime);
Console.ReadLine();
}
}
}
ログイン後にコピー

出力:

C# の日時

例 #2

たとえば、ユーザーからの入力として年を取得し、DateTime.IsLeap Year() メソッドを使用してそれがうるう年かどうかを確認します。

コード:

using System;
using System.IO;
namespace ConsoleApp4
{
class Program
{
public static void Main()
{
try
{
//taking year as input from user
Console.WriteLine("Please enter a year");
int year = Convert.ToInt32(Console.ReadLine());
//checking if entered year is a leap year or not
//using DateTime.IsLeapYear() method
Console.WriteLine("\n Using IsLeapYear() method:");
if (DateTime.IsLeapYear(year))
{
Console.WriteLine(year + " is a leap year");
}
else
{
Console.WriteLine(year + " is not a leap year");
}
//checking if entered year is a leap year or not
//using DateTime.DaysInMonth() method
Console.WriteLine("\n Using DaysInMonth() method:");
if (DateTime.DaysInMonth(year, 2) == 29)
{
Console.WriteLine(year + " is a leap year");
}
else
{
Console.WriteLine(year + " is not a leap year");
}
}
catch(Exception ex)
{
Console.WriteLine(ex.Message);
}
Console.ReadLine();
}
}
}
ログイン後にコピー

出力:

C# の日時

例 #3

たとえば、年の最初と最後の日を取得します。

コード:

using System;
using System.IO;
namespace ConsoleApp4
{
class Program
{
public static void Main()
{
DateTime dateTime = DateTime.Now;
//displaying first day of current year
DateTime firstDay = new DateTime(dateTime.Year, 1, 1);
Console.WriteLine("First day of {0} is {1}", dateTime.Year, firstDay);
//getting first day of next year
DateTime dateTimeNext = new DateTime(dateTime.Year + 1, 1, 1);
//subtracting one day from the first day of next year
//to get the last day of current year
DateTime lastday = dateTimeNext.AddDays(-1);
Console.WriteLine("Last day of {0} is {1}", dateTime.Year, lastday);
Console.ReadLine();
}
}
}
ログイン後にコピー

出力:

C# の日時

結論

dateTime 構造体は、日付と時刻を操作するために使用されます。日付と時刻を格納するデータ型として使用されます。 DateTime は、日付と時刻を操作するためのプロパティとメソッドを提供します。 DateTime は構造体であり、値型です。 null にすることはできません。

以上がC# の日時の詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。

関連ラベル:
ソース:php
このウェブサイトの声明
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。
最新の問題
人気のチュートリアル
詳細>
最新のダウンロード
詳細>
ウェブエフェクト
公式サイト
サイト素材
フロントエンドテンプレート
私たちについて 免責事項 Sitemap
PHP中国語ウェブサイト:福祉オンライン PHP トレーニング,PHP 学習者の迅速な成長を支援します!