CodePanic! > C#.NET Tips > 今ここ
■日付と時刻を指定書式に変換する
DateTime構造体のToString関数を使います。
ToString関数の引数で以下の文字を使って書式を指定します。
yy 西暦下2桁
yyyy 西暦4桁
M 月(1〜12)
MM 月(01〜12)
d 日(1〜31)
dd 日(01〜31)
ddd 曜日(日〜土)
dddd 曜日(日曜日〜土曜日)
tt 午前、午後
h 時(1〜12)
hh 時(01〜12)
H 時(0〜23)
HH 時(00~23)
m 分(0〜59)
mm 分(00〜59)
s 秒(0〜59)
ss 秒(00〜59)
using System;
using System.Collections.Generic;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
// 現在の日付と時刻を取得
DateTime date = DateTime.Now;
// 日付と時刻を指定書式に変換する
Console.WriteLine(date.ToString("yyyy/MM/dd HH:mm:ss"));
}
}
}
【結果】
2008/08/05 11:02:11
Copyright © 2008.07 - shougo suzaki
|