C# で現在の時刻を取得する
Muhammad Maisam Abbas
2021年5月23日
2021年5月9日

このチュートリアルでは、C# の文字列変数で現在の時刻を取得する方法について説明します。
C# の DateTime.Now
プロパティで現在の時刻を取得する
DateTime
構造体は、C# の時間のインスタンスを表します。DateTime
構造体の DateTime.Now
プロパティは、ローカル時間で表されたローカルマシンの現在の日付と時刻を取得します。C# の DateTime.ToString()
メソッドを使用して、DateTime.Now
プロパティの結果を文字列変数に変換できます。次のコード例は、C# の DateTime.Now
プロパティを使用して、文字列変数でローカルマシンの現在の時刻を取得する方法を示しています。
using System;
namespace get_current_time
{
class Program
{
static void Main(string[] args)
{
string datetime = DateTime.Now.ToString("hh:mm:ss tt");
Console.WriteLine(datetime);
}
}
}
出力:
02:57:57 PM
ローカルマシンの現在の時刻を、DateTime.Now
プロパティと C# の DateTime.ToString()
関数を使用して、datetime
文字列変数内の hh:mm:ss tt
形式で保存しました。
Author: Muhammad Maisam Abbas
Maisam is a highly skilled and motivated Data Scientist. He has over 4 years of experience with Python programming language. He loves solving complex problems and sharing his results on the internet.
LinkedIn