[C#]標準の日時書式指定文字列のサンプル

2021年10月6日

書式
public static DateTime Today { get; }
現在の日付を取得します。
d 短い形式の日付パターン 2022/05/12
D 長い形式の日付パターン 2022年5月12日
g 一般の日付と時刻のパターン 2022/05/12 20:30
Y 年月パターン 2022年5月
M 月日パターン 5月22日
使用例

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
// 現在の日付
DateTime cftDay = DateTime.Today;
// 日付を一般形式で表示
Console.WriteLine(cftDay.ToString());
Console.WriteLine();
// 短い形式の日付パターン
Console.WriteLine("短い形式: " + cftDay.ToString("d"));
// 長い形式の日付パターン
Console.WriteLine("長い形式: " + cftDay.ToString("D"));
//一般の日付と時刻のパターン
Console.WriteLine("一般の日付: " + cftDay.ToString("g"));
//年月パターン
Console.WriteLine("年月: " + cftDay.ToString("Y"));
//月日パターン
Console.WriteLine("月日: " + cftDay.ToString("M"));
Console.ReadKey();
}
}
}
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace ConsoleApplication1 { class Program { static void Main(string[] args) { // 現在の日付 DateTime cftDay = DateTime.Today; // 日付を一般形式で表示 Console.WriteLine(cftDay.ToString()); Console.WriteLine(); // 短い形式の日付パターン Console.WriteLine("短い形式: " + cftDay.ToString("d")); // 長い形式の日付パターン Console.WriteLine("長い形式: " + cftDay.ToString("D")); //一般の日付と時刻のパターン Console.WriteLine("一般の日付: " + cftDay.ToString("g")); //年月パターン Console.WriteLine("年月: " + cftDay.ToString("Y")); //月日パターン Console.WriteLine("月日: " + cftDay.ToString("M")); Console.ReadKey(); } } }
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {

            // 現在の日付
            DateTime cftDay = DateTime.Today;

            // 日付を一般形式で表示
            Console.WriteLine(cftDay.ToString());
            Console.WriteLine();

            // 短い形式の日付パターン
            Console.WriteLine("短い形式: " + cftDay.ToString("d"));
            // 長い形式の日付パターン
            Console.WriteLine("長い形式: " + cftDay.ToString("D"));
            //一般の日付と時刻のパターン
            Console.WriteLine("一般の日付: " + cftDay.ToString("g"));
            //年月パターン
            Console.WriteLine("年月: " + cftDay.ToString("Y"));

            //月日パターン
            Console.WriteLine("月日: " + cftDay.ToString("M"));

            Console.ReadKey(); 
        }
    }
}

実行結果
2021/10/06 0:00:00

短い形式: 2021/10/06
長い形式: 2021年10月6日
一般の日付: 2021/10/06 0:00
年月: 2021年10月
月日: 10月6日

C#

Posted by arkgame